Areas is one of the many new features introduced in the ASP. 2.0 release, which can help you divide a larger Web project into several components, area. The ability to implement area can have two organizational forms:
- Create a areas in 1 ASP. 2.0 project.
- Create multiple ASP. 2.0 project, each project is an area.
The 2nd kind of structure is more complex, but the 1th structure can also achieve the parallel development between each area, do not affect each other, so the 2nd method is not recommended.
For example, the default template for ASP. NET MVC 2.0:
1. first create a new project for ASP. NET MVC 2.0, click the right mouse button on project to select Add->area, enter the name of area in the open window (for example: Profile), click the Add button, You will then see the structure below.
The structure of the area where the
name is called profile is the same as the structure of the controllers,models and views under the root directory, and the only difference is that there is a ProfileAreaRegistration.cs file underneath the profile. It inherits from arearegistration class, profilearearegistration must implement the Span style= "color: #008080;" AreaName property in the >arearegistration class and Registerarea (
using SYSTEM.WEB.MVC; namespace ASP.NET_Mvc_2_Test.Areas.Profile { public class profilearearegistration: arearegistration { PU Blic override string AreaName { Get{return "profile";} } public override void Registerarea ( AreaRegistrationContext context) {context. MapRoute ( "Profile_default", "profile/{controller}/{ Action}/{id} ", new {action = urlparameter.optional}); } } }
The AreaName property is used to define the name of the area, and the Registerarea (arearegistrationcontext context) method can be seen in the browser's address bar where the URL is styled as profile/{ Controller}/{action}/{id}, is a 4-level construct, as long as the context. MapRoute (...) instead
public override void Registerarea (arearegistrationcontext context) { context. MapRoute ( urlparameter.optional});}
The style of the URL is then changed to a level three structure profile/{action}/{id}.
2. Modify the Views/shared/site.master file in the root directory and add a menu item named o "Your profile" and specify the name of area, which is the name of the area in the example.
<UlId= "Menu" > <Li><%= Html.ActionLink ("Home","Index","Home",New {area =""},Null%></Li> <Li><%= Html.ActionLink ("Your profile","Viewprofile","Profile",new {area = null) Span style= "background: #ffee62;" >%></li> <li><%= Html.ActionLink ( "Home", new {area = ""}, null) %></li> </ul>
Note: Home and about do not belong to any area, but you also want to declare the area through an anonymous object, and if you do not declare area, when you enter a view of profile, the URL of home and about will change to Profile/home. Profile/about, when you click Home or about, there will be an exception, so when a link on the page does not belong to any area and is likely to be available in more than one area, be sure to add new {area = "}
3. only match the correct route to show that the route in the View,area in area is already configured, but how is it added to the routetable?
The project is divided into three first page
such as:/home/index front page
/admin/home/index Backstage Home
/oa/home/index Office Platform Home
Create a new ASP. MVC3 Sample Project: Right-click → add →area
Run the project directly:
The reason is that there are multiple controllers with the same name, and you need to configure the default namespace. Workaround:
Open Global.asax.cs
public static void RegisterRoutes (RouteCollection routes) {routes. Ignoreroute ("{resource}.axd/{*pathinfo}");
Routes. MapRoute ("Default",//Route name "{controller}/{action}/{id}",//URL with parameters new {cont roller = "Home", action = "Index", id = urlparameter.optional},//Parameter defaults new[] {"Web.controllers "}//namespaces introduce the default namespace);
}
http://localhost:49849/ post-run output home/index
Http://localhost:49849/Admin/Home/Index post-run output admin/home/index
Http://localhost:49849/OA/Home/Index post-run output oa/home/index
To change the path:
http://localhost:49849/Admin/ after www.2cto.com 404 error
The reason is that the admin under area does not have the default controller configured, the workaround:
Open area under admin under AdminAreaRegistration.cs
New { controller = "Home", action = "Index", id = urlparameter.optional}
Add the default controller.