Activity: Implementing Your Own IActionResult

Scenario

You want to learn how to access the underlying stream of response of a string.

Aim

Write an activity result that would capitalize the given string.

Steps for completion

  1. First, add the following class to your projects:

Go to https://goo.gl/GDi6JS to access the code.
public class UpperStringActionResult : ActionResult
{
readonly string str;
public UpperStringActionResult(string str)
{
this.str = str;
}
public override void ExecuteResult(ActionContext context)
{
var upperStringBytes =
Encoding.UTF8.GetBytes(str.ToUpper());
context.HttpContext.Response.Body.Write(
upperStringBytes, 0, upperStringBytes.Length);
}
}
What is encoding? Encoding is basically a process in which a sequence of characters is put into a specialized format. The characters could be numerical, alphabet, symbols, and so on. The purpose is to serve efficient transmission and storage. What is UTF-8?  UTF-8 is the encoding for the web for efficiency reasons.
  1. Then, revise your controller action, as follows:

Go to https://goo.gl/DTWzN4 to access the code.
public IActionResult IndexUpper()
{
return new UpperStringActionResult("Hello World! I am learning MVC!");
}
  1. Then, run your application. You'll get the following output:

As you can see, all letters are in capitals.