How to Configure Areas in ASP.NET MVC3
Asked Answered
R

3

36

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.

Respect answered 9/3, 2011 at 8:38 Comment(0)
A
40

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();
Allister answered 9/3, 2011 at 9:25 Comment(9)
thanks for the answer.How can i know that the Area is Registered properly?. I used this link in my shared _layout but it's not taking me anywhere @Html.ActionLink("Blog", "About", "Home", new { area = "Blog"})Respect
@Imrul, from what I can see in your code your area is called Blogs, not Blog, so try: @Html.ActionLink("Blog", "About", "Home", new { area = "Blogs" }). Also make sure that there is a HomeController inside this area.Allister
sorry, for the spelling mistake. I think i got my problem, if i render this to my _layout it doesn't generate a link to Area/Controller/Action "@Html.ActionLink("Blog", "Index", "BlogHome", new { area = "Blogs"})". Am i doing anything wrong in ActionLink helper? and i discover now that same controller name can't be possible in root and Area. For that i had to rename Home to BlogHome. and FYI <a href="/?Length=8" area="Blogs">Blog</a> is the Generated html and using localhost:4135/Blogs/BlogHome/Index hits the BlogHome controller and shows the page.Respect
if i use @Html.ActionLink("Blog", "Index", "Blogs/BlogHome") then it works. But is it the right way ?Respect
Your using wrong overload, try this one: @Html.ActionLink("Blog", "Index", "BlogHome", new { area = "Blogs" }, null)Csch
thanks, @frennky. It works like a charm. Would you please give me any link, where can i find these changes. I Googled it but there is no help. Where can i find all the functions/helpers of ASP.NET MVC3?Respect
@Respect - in answer to your question about checking if your area registration is correct, I'd suggest two things 1) make sure your web project is running with the "Use Visual Studio Development Server", then place breakpoints where you need e.g. end of application_start, in your area registration methods etc. 2) Use Glimpse to view your routes - you'll see what's in the route collection, and what the current url is matched againstParsaye
@DarinDimitrov, is it necessary to specify area explicit when using Html.ActionLink if area is the same as that in which is view located?Induce
@EvgenyLevin, no, if you are displaying a view in the same area you don't need to specify it.Liberec
B
5

You can have the same controller name in the root and the area, you just have to define it.

In your global.asax, add the last line of the routes.maproute as shown below

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

also, add the name of the controller in your ares/?????AreaRegistration.cs file

 context.MapRoute(
        "Membership_default",
        "Membership/{controller}/{action}/{id}",
         new { controller= "Home", action = "Index", id = UrlParameter.Optional }
      );
Byrnes answered 24/9, 2011 at 15:3 Comment(0)
J
1

please find below image shows how to configure area in mvc .enter image description here

Jamikajamil answered 11/11, 2014 at 13:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.