- 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.
- Flask Web全棧開發實戰
- Getting Started with PowerShell
- Building Mapping Applications with QGIS
- PLC編程及應用實戰
- ANSYS Fluent 二次開發指南
- Java Web開發詳解
- Mastering openFrameworks:Creative Coding Demystified
- Python程序設計開發寶典
- IoT Projects with Bluetooth Low Energy
- Go語言從入門到精通
- .NET 4.0面向對象編程漫談:應用篇
- C# 7.1 and .NET Core 2.0:Modern Cross-Platform Development(Third Edition)
- Mastering Node.js
- PhoneGap 3.x Mobile Application Development Hotshot
- C++游戲設計案例教程