官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 乌拉特中旗| 绥化市| 龙口市| 育儿| 漳浦县| 临海市| 罗定市| 通渭县| 明光市| 咸丰县| 玉屏| 西城区| 扎囊县| 万山特区| 汕尾市| 沛县| 垣曲县| 文山县| 萨迦县| 英山县| 巢湖市| 中西区| 泸定县| 隆昌县| 永福县| 安国市| 文昌市| 恩施市| 塘沽区| 保靖县| 顺平县| 平阴县| 临泽县| 方正县| 汨罗市| 称多县| 奇台县| 芜湖县| 孝昌县| 齐齐哈尔市| 五华县|