- Modern Web Development with ASP.NET Core 3
- Ricardo Peres
- 599字
- 2021-06-18 18:36:03
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()
);
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!).
Let's carry on by exploring the view class.
- DevOps:軟件架構師行動指南
- 零基礎學Visual C++第3版
- 人臉識別原理及算法:動態人臉識別系統研究
- AppInventor實踐教程:Android智能應用開發前傳
- MATLAB 2020從入門到精通
- Mastering Git
- Python Data Science Cookbook
- Training Systems Using Python Statistical Modeling
- 跟戴銘學iOS編程:理順核心知識點
- scikit-learn Cookbook(Second Edition)
- DB2SQL性能調優秘笈
- Flutter從0基礎到App上線
- 樹莓派開發從零開始學:超好玩的智能小硬件制作書
- Visual FoxPro數據庫程序設計
- JavaScript重難點實例精講