Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Configure Areas in ASP.NET MVC3

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.

like image 580
Imrul Avatar asked Mar 09 '11 08:03

Imrul


People also ask

How can use area in ASP NET MVC?

Add MVC Area with Visual StudioIn Solution Explorer, right click the project and select ADD > New Scaffolded Item, then select MVC Area. Areas are an ASP.NET feature used to organize related functionality into a group as a separate namespace (for routing) and folder structure (for views).

Can you identify the correct steps for adding area to ASP NET core application?

Create ASP.NET Core MVC Area To add a new Area, Right Click on application name from solution explorer -> Select Add -> Select New Scaffolded Item -> select MVC Area from middle pane of dialog box -> Enter Name of Area -> Click Ok.

What is ASP Net mvc3?

ASP.NET MVC 3 is a framework for developing highly testable and maintainable Web applications by leveraging the Model-View-Controller (MVC) pattern.

What is Area C#?

Areas allows you to separate your modules and organize Model, View, Controller, Web. config and Routing registration file into separate sections. Why we use Areas in MVC. In live MVC Project implementation we can use Areas concept for organizing project in better manageable way.


3 Answers

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();
like image 187
Darin Dimitrov Avatar answered Oct 21 '22 15:10

Darin Dimitrov


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 }
      );
like image 5
shamshir Avatar answered Oct 21 '22 14:10

shamshir


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

like image 1
user1089766 Avatar answered Oct 21 '22 15:10

user1089766