Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Is anyone knows how to Configure Areas in ASP.NET MVC3. I read an article about Areas in here. But that article is not based on MVC3. In MVC3 there is no function named MapRootArea in RouteCollection routes which is found in Global.asax

routes.MapRootArea("{controller}/{action}/{id}", 
                 "AreasDemo", 
                  new { controller = "Home", action = "Index", id = "" });

When i create a New Area using MVC3, i got a class of that area which inherited from AreaRegistration and look like following: (here Blogs is the area name)

public class BlogsAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Blogs";
        }
    }

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

Would anyone please help me how do i configure area in MVC3. Any kind of link would be helpful also.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
162 views
Welcome To Ask or Share your Answers For Others

1 Answer

Right click on your web project and select Add -> Area... Then type the name of the area and Visual Studio will take care of the rest which is to generate all the necessary classes. For example the area registration might look like this:

public class AreasDemoAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "AreasDemo";
        }
    }

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

and in Application_Start of your Global.asax all you need is:

AreaRegistration.RegisterAllAreas();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...