Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • ASP.NET MVC Areas

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 300
    Comment on it

    We already know that ASP.NET MVC framework includes separate folders for Model, View and Controller. However a large application can include a large number of controller, views and model which is difficult to manage. Areas help in splitting such large ASP.NET MVC application into smaller sub-applications.
    We can create an Area using Visual Studio by right clicking on the project in the solution explorer and choosing the following option : => Add => Area..


    In the solution explorer we can see, each area includes AreaRegistration class, in the fromat [{area name} + AreaRegistration].cs file.Below is a sample file after adding an area by the name user.

    public class userAreaRegistration : AreaRegistration 
    {
        public override string AreaName 
        {
            get 
            {
                return "user";
            }
        }
    
        public override void RegisterArea(AreaRegistrationContext context) 
        {
            context.MapRoute(
                "user_default",
                "user/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }
            );
        }
    }
    

    AreaRegistration class overrides RegisterArea method to map the routes for the area. In the above example, if the url starts with user it will be handled by the controllers included in the user folder located in Area folder.
    You will have one area registration class file for each area and alll the areas must be registered in Application_Start event in Global.asax.cs as

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas(); 
        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }
    

    Thus by using areas we can split our application into manageable parts.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: