MVC4 Note Area

Source: Internet
Author: User

mvc4.0 New Area zone mechanism, can assist you in the architecture of larger projects, so that the more independent part of the function into an MVC sub-site, to reduce the coupling between the site and the site, can also be cut through the area, so that many people at the same time to develop the same project, can reduce conflicting opportunities.

  cannot have two controllers with the same name in an MVC project, even if you have a distinction between namespaces that can be compiled correctly, but in fact still error at run time, unless you add a new URL route setting in the App_start/routeconfig file through the Maproute method. and explicitly specify a namespace for this feature to work correctly. Egroutes. MapRoute (Name: "Order System", URL: "Orders/{controller}/{action}/{id}", defaults:new {controller = "Member" action= "index", ID =urlparameter.optional},namespaces:new string[]{"mvcapplication1.controllers. Order System"}); routes. MapRoute (Name: "Membership System", URL: "Members/{controller}/{action}/{id}", defaults:new {controller = "Member" action= "index", Id=urlparameter.optional},namespaces:new string[]{"mvcapplication1.controllers. Member System"});  but if the site is really big, Perhaps the controller is good dozens of files, so continuous maintenance, the site will become more and more complex, lost the benefits of separation of concerns, MVC project if the site is highly independent, usually split into different projects to develop, but in management will become more troublesome, For example, in different projects there will be a duplicate Web. config property, and the complexity of the deployment increases.   also because of this need, in 2.0 MVC provides the area mechanism, can be in the same project can be able to slice different MVC sites, and each subsite will have a complete MVC directory structure in developers like the district divided into different MVC sites   How to add a new zone item to an existing project right-click the area    how to set the URL of a zone there will be a [areaname] in each zone directory. AreaRegistration.cs documents For example, if a zone has just been added, there will be a aAreaRegistration.cs, which will define the URL route as if it were app_start/routeconfig.cs. AAreaRegistration.cs inside this class setArearegistration abstract class, and replace a AreaName property to specify the name of the zone, and replace a method named Registerarea, which is the property of the app_start/ The RegisterRoutes method in RouteConfig.cs is the same.  http://blog.sina.com.cn/s/blog_95fa28e60102vdn7.html the priority of the default route and zone route is application_start in Global.asax in the site root directory ( ) method will see the Arearegistration.registerallarea (); The default program code   where the first line Arearegistration.registerallareas () That is, the area of the route registration Gold Routetable.routes, "first Register first win" If a site route is defined in the AAreaRegistration.cs of Zone A, and the same rule is registered in Global.asax at the root of the site, then the rule of success will be the route of the zone.    How to make a link between a default website and a regional site in MVC There are three default RouteValue names, namely controller, Action,area, where the controller and action parameters are necessary parameters, integral, The rest of the area is a routevalue that must be added when it is necessary to make a special area. Take Html.ActionLink () as an example, (as in Url.action's usage)   to generate the index action of the member controller under the specified a zone, use the following syntax, As long as an additional zone route value can be @html.actionlink ("member order", "Index", new {controller= "Member", Area= "a"})   @Html. Action ("Adetail", "Member", new {area= "a"}) if you want to load part of the monitoring property with the Html.action helper method

MVC4 Note Area

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.