Conventions in ASP. NET MVC

Source: Internet
Author: User

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 classHomeController : 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

123 @{    Layout = null;}

Source: Conventions in ASP. NET MVC

Conventions in ASP. NET MVC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.