There are many default conventions in an ASP. NET MVC project. Includes controller conventions, view conventions, and so on.
First, the Controller convention
1. Controller must be non-static class
2, controller must implement Interface IController
3, the Controller's class name must end with a controller, such as HomeController;
As below is a standard controller:
1 |
public class
HomeController : Controller
|
4, when referring to the controller in the project, for example, using the HTML Renderaction method, you only need to specify the first half of the name, such as home;
1 |
@Html. Renderaction ("Check", "Home", null); //home is controller |
5, you can implement the Icontrollerfactory interface to create your custom-made conventions;
Second, the View Convention
1. View or partial view to be placed in the/views/controllername folder;
For example, the HomeController view is always placed inside the/views/home folder;
650) this.width=650; "src=" Http://www.mashangpiao.net/upload/image/20160818/6360711789320068352715011.png "title= "2.png" alt= "2.png" style= "border:2px solid rgb (220,220,220); height:auto;margin:16px 0px;"/>
Of course, after being familiar with MVC, you can customize the path of views.
2. The default view of the action method has the same name as action;
For example, if there is a content action in HomeController, the default view of content is/views/home/content.cshtml.
When we call, if we do not pass the parameter return view (), then the content view is used by default. If we want to use a different view, we can pass in the view name parameter, such as Return View ("index"), and use the/views/home/index view to render.
3. Find the order of the views
The default order of the ASP. NET MVC Lookup View path is:
1,/views/controlloername/;
2,/views/shared/;
If the above two places are not found to find the view, then an error;
For example, we visit/home/content. The order of the default lookup views is as follows:
650) this.width=650; "src=" Http://www.mashangpiao.net/upload/image/20160818/6360711749928939152820884.png "title= "1.png" alt= "1.png" style= "border:2px solid rgb (220,220,220); height:auto;margin:16px 0px;"/>
Third, the arrangement of the Convention
1, the layout file needs to add "_" prefix. and stored under the/views/shared folder. When we create a view, _layout.cshtml is used by default as a layout template.
If you don't want a single page of the template, you can choose not to use master pages, or add the following code at the top of the page
Source: Conventions in ASP. NET MVC
Conventions in ASP. NET MVC