- ASP.NET Core 2 Fundamentals
- Onur Gumus Mugilan T. S. Ragupathi
- 181字
- 2021-07-23 17:09:46
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
- 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.
- 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!");
}
- Then, run your application. You'll get the following output:
As you can see, all letters are in capitals.
推薦閱讀
- Learn TypeScript 3 by Building Web Applications
- Delphi程序設計基礎:教程、實驗、習題
- DevOps入門與實踐
- Koa開發:入門、進階與實戰
- Getting Started with Python Data Analysis
- 零基礎學單片機C語言程序設計
- PHP+Ajax+jQuery網站開發項目式教程
- OpenMP核心技術指南
- Oracle Data Guard 11gR2 Administration Beginner's Guide
- Python Deep Learning
- Python程序設計教程
- Sitecore Cookbook for Developers
- Mastering Magento Theme Design
- JSP程序設計與案例教程
- Go Programming Cookbook(Second Edition)