Default MVC conventions

Source: Internet
Author: User

There are many default conventions in the MVC project. One is the Convention for allocating project directories. For example, by default, Javascript files need to be placed in the Script folder, however, this does not prevent you from renaming the folder. You can also place the entire folder in any place you want to place without affecting the normal operation of the program. The other is to follow the so-called principle of convention over configuration. For many projects, strictly Following existing conventions and using reasonable default options is probably required by these projects, which means that if a set of naming rules are established, suchSignificantlyReduce the number of configuration items required by the system to establish processor ing, view parser,ModelAndViewInstance, and so on. This provides great convenience for rapid prototyping. At the same time, it provides a certain degree of (usually a good thing) Consistency of the code base, from which you can choose to develop concurrency into a Molding Product.

---- Provisions on the Controller class

  • The Controller class name must end with the Controller; for example, ProductController
  • When referencing a controller in a project, for example, when using the HTML helper method, you only need to specify the name of the first half; for example, Product;
  • You can create custom conventions by implementing the IControllerFactory interface, although this is not necessary;

---- View conventions

  • The view or segment view should be placed in the/Views/Controllername folder. For example, a view related to the ProductController class should be placed in the/Views/Product folder;
  • The MVC Framework expects that the default view of an action method should be named by the method name. For example, the view of a List method should be named List. cshtml. Because the List method is located in the ProductController class, the default view should be/Views/Product/List. cshtml.
  • If return View () is directly called in the action method, the default View is displayed. If other specified views are displayed, return View ("MyOtherView") must be called in the action method ");
  • By default, the MVC Framework searches for view paths in the/Views/Controlloername/folder. If no view path is found, search for it in the/Views/Shared/folder, an error is reported;

---- Layout conventions

  • The Layout file must be prefixed with "_" in the file and placed in the/Views/Shared Folder. By default, _ Layout is used for all project templates except empty. cshtml is used as the layout template and in/Views/_ ViewStart. cshtml.
  • If you want to replace the specified layout, modify _ ViewStart. cshtml:
@{            Layout="~/Views/Shared/MyLayout.cshtml";}
  • To disable the layout, modify _ ViewStart. cshtml:
@{       Layout=null;     }

 

 

 

 

 

 

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.