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

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.

主站蜘蛛池模板: 芮城县| 犍为县| 尉犁县| 黑山县| 昔阳县| 个旧市| 新泰市| 冀州市| 松潘县| 萍乡市| 河间市| 满洲里市| 黎城县| 娄底市| 肥乡县| 南陵县| 长治县| 阿克苏市| 康马县| 梅州市| 固阳县| 镇远县| 玛沁县| 八宿县| 都江堰市| 凤庆县| 石台县| 梅河口市| 巴里| 章丘市| 哈巴河县| 盐津县| 清水河县| 肥乡县| 吴忠市| 绥阳县| 乌拉特后旗| 峡江县| 宁城县| 临猗县| 兴仁县|