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

Getting started

Views are the V in MVC. They are the visual part of the application. Typically, a web app renders HTML pages, meaning HTML views. A view is a template that consists of a mix of HTML and possibly some server-side content.

ASP.NET Core uses view engines to actually render the views, an extensible mechanism. Before the time of Core, there were several view engines available; although their purpose was always to generate HTML, they offered subtle differences in terms of syntax and the features they supported. Currently, ASP.NET Core only includes one view engine, called Razor, as the other one that used to be available, Web Forms, was dropped. Razor has been around for quite some time and has been improved in the process of adding it to ASP.NET Core.

Razor files have the cshtml extension (for C# HTML) and, by convention, are kept in a folder called Views underneath the application, and under a folder with the name of the controller to which they apply, such as Home. There may be global and local views, and we will learn the distinction in a moment.

The typical way to have a controller action returning a view is by returning the result of executing the View method of the Controller class. This creates ViewResult, and it can take a number of options, as follows:

  • ContentType (string): An optional content type to return to the client; text/html is the default
  • Model (object): Just any object that we want to make available to the view
  • StatusCode (int): An optional status code to return; if none is provided, it will be 200
  • TempData (ITempDataDictionary): Strongly typed temporary data to make available until the next request
  • ViewData (ViewDataDictionary): A key-value collection of arbitrary data to pass to the view
  • ViewName (string): The name of the view to render

The only required parameter is ViewName, and, if it's not supplied, the current action name will be used; that is to say, if we are executing in an action method named Index, and we want to return a view but don't supply its name, Index will be used, as illustrated in the following code snippet:

public IActionResult Index()
{
return this.View(); //ViewName = Index
}

There are some overloads to the View method that basically take either the viewName, the model, or both, as illustrated in the following code snippet:

return this.View(
viewName: "SomeView",
model: new Model()
);
Beware—if your model is of the string type, .NET may mistakenly choose the View overload that takes a view name!

Now, imagine you want to return a view with a specific content type or status code. You can get the ViewResult object from the View method call and then change it, like this:

var view = this.View(new Model());
view.ContentType = "text/plain";
view.StatusCode = StatusCodes.Status201Created;
return view;

Or, if we want to set some view data, we can run the following code:

view.ViewData["result"] = "success";

One thing that you must not forget upfront is, if you have not registered your MVC services with AddMvc, you will need to do so with AddControllersWithViews, like this:

services.AddControllersWithViews();

This will result in slightly less memory pressure than AddMvc because it will not, for example, register the services that are needed for Razor pages (do not confuse them with Razor views, the scope of this chapter!).

Razor Pages and Razor views are not the same thing: Razor Pages are callable on their own, whereas Razor views are returned by controller action methods. Razor Pages will be discussed in their own chapter.

Let's carry on by exploring the view class.

主站蜘蛛池模板: 西安市| 尼木县| 双辽市| 庄浪县| 苍南县| 山东省| 西乌| 西充县| 塔河县| 青神县| 库伦旗| 汉中市| 通许县| 休宁县| 威宁| 商都县| 静宁县| 尚义县| 丰都县| 开化县| 若尔盖县| 遵化市| 丘北县| 伊川县| 东乡| 淮滨县| 乌拉特前旗| 汕尾市| 株洲县| 博野县| 潮安县| 虞城县| 新巴尔虎右旗| 大丰市| 广饶县| 嘉鱼县| 铜陵市| 囊谦县| 昭苏县| 当雄县| 双桥区|