Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
AreaRegistration Class
Collapse the table of content
Expand the table of content

AreaRegistration Class

.NET Framework 4

Provides a way to register one or more areas in an ASP.NET MVC application.

System.Object
  System.Web.Mvc.AreaRegistration

Namespace:  System.Web.Mvc
Assembly:  System.Web.Mvc (in System.Web.Mvc.dll)

public abstract class AreaRegistration

The AreaRegistration type exposes the following members.

  NameDescription
Protected methodAreaRegistrationInitializes a new instance of the AreaRegistration class.
Top

  NameDescription
Public propertyAreaNameGets the name of the area to register.
Top

  NameDescription
Public methodEqualsDetermines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodStatic memberRegisterAllAreas()Registers all areas in an ASP.NET MVC application.
Public methodStatic memberRegisterAllAreas(Object)Registers all areas in an ASP.NET MVC application by using the specified user-defined information.
Public methodRegisterAreaRegisters an area in an ASP.NET MVC application using the specified area's context information.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

When you add an area to an ASP.NET MVC application, Visual Studio creates a file named AreaRegistration. The file contains a class that derives from AreaRegistration. This class defines the AreaName property and the RegisterArea method, which registers the route information for the new area.

The following example shows the AreaRegistration class created for a new area named Blog.

public class BlogAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Blog";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Blog_default",
            "Blog/{controller}/{action}/{id}",
            new { action = "Index", id = "" }
        );
    }
}

The Global.asax file contains the RegisterRoutes method that is called when an ASP.NET MVC application starts. The RegisterRoutes method of any application that implements areas must contain a call to the RegisterAllAreas method.

The following example shows a RegisterRoutes method that contains a call to RegisterAllAreas.

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    AreaRegistration.RegisterAllAreas();
    routes.MapRoute(
        "Default",                      // Route name
        "{controller}/{action}/{id}",   // URL with parameters
        new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
    );
}
[Visual Basic]
Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
    AreaRegistration.RegisterAllAreas()
    routes.MapRoute( _
        "Default", _
        "{controller}/{action}/{id}", _
        New With {.controller = "Home", .action = "Index", .id = ""} _
    )

End Sub

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

ADD
Show:
© 2015 Microsoft