Areas in ASP.Net MVC2,3

Areas provide a way to separate a large MVC Web application into smaller functional groupings. 
An area is effectively an MVC structure inside an application. 
An application could contain several MVC structures (areas).


How to add areas?

  • In Solution Explorer, right-click the project name, click Add, and then click Area.
  • In Area Name, type [AreaName] and then click Add.
  • An Areas folder is added to the project.
The Areas folder contains a folder structure that allows each child area to have its own models, views, and controllers.


Registering Area Routes

Global.asax.cs:       
 protected void Application_Start()
 {
       AreaRegistration.RegisterAllAreas();

       RegisterGlobalFilters(GlobalFilters.Filters);
       RegisterRoutes(RouteTable.Routes);
  }
This code calls the route registration methods for each child area.

AreaNameAreaRegistration.cs is automatically added when the area structure is generated, and the default routing is defined.
 public class AreaNameAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "AreaName";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "AreaName_default",
                "AreaName/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }
            );
        }
    }

Remark

After adding an Area, you need to do one more modification in Global.asax.cs, add the Main controller namespace into the MapRoute method:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
                new string[] { "AreaDemo.Controllers" }
            );

        }

Otherwise, if there is name conflict between Main controller and Area controller, when you target Main controller, there will be an error:

Multiple types were found that match the controller named 'Test'. This can happen if the route that services this request ('{controller}/{action}/{id}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.
 
The request for 'Test' has found the following matching controllers:
AreaDemo.Areas.AreaName.Controllers.TestController
AreaDemo.Areas.AnotherAreaName.Controllers.TestController
AreaDemo.Controllers.TestController

To keep coding consistance, better to modify the MapRoute method in the AreaRegistration as well.

 public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "AreaName_default",
                "AreaName/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional },
                new string[] { "AreaDemo.Areas.AreaName.Controllers" }
            );
        }

Linking Between Areas

To generate a link to a different area, you must explicitly pass the target area name in the routeValues parameter for these methods. 
e.g.
<%= Html.ActionLink("LinkText", "ActionName", "ControllerName", new { area = "AreaName" }, null) %>

posted on 2011-06-23 01:44  Cooldash  阅读(735)  评论(7)    收藏  举报

导航