Click here to Skip to main content
15,885,032 members
Articles / Web Development / ASP.NET

How to Create an Area in ASP.NET MVC Application

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
15 Oct 2016Ms-PL3 min read 35.3K   5   1
This post shows how to create an area in ASP.NET MVC application

Areas in ASP.NET MVC allows us to separate a Web Application into smaller modules and organize the code. For example, if you have a blog feature in your application, you can create that as a separate area and that gives flexibility to maintain the blog code separately.

Steps for Creating an Area in ASP.NET MVC Project

  1. Create a new ASP.NET MVC Web Application.

    image

  2. Choose empty template to start with and click on OK.

    image

  3. Now you should be able to see a solution with ASP.NET MVC web application project.

    image

  4. Right click on the project, click Add and click on Area.

    image

  5. Enter the Area name and click Add.

    image

  6. Area gets created and your solution looks something like below:

    image

  7. Build your project, right click on Controllers folder of MainApplication, click on Add and then click on controller.

    image

  8. Enter the Controller name and click on Add.

    image

  9. Similarly, add Controller to Areas > Blog > Controllers. Your solution should look like below:

    image

  10. As you can see, I have two Controllers, one in the Main Application and one in the Area called Blog with the same name HomeController. This is possible because both the Controllers have different NameSpaces, i.e., MainApplication.Controllers and MainApplication.Areas.Blog.Controllers respectively.
  11. Now that we have Controllers created, right click on Index action and click Add View.

    image

  12. Similarly Add View, to the Blog areas HomeController Index action. Your solution should look like below:

    image

  13. Build your application hitting Ctrl + Shift + B and then hit Ctrl + F5 to run it. When you run it, if you are using the Controller with the same name like I did, i.e., HomeController, you should get the below error.
    Multiple types were found that match the controller named 'Home'. This can happen if the route that services this request ('{controller}/{action}/{id}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.
  14. To fix the above, go to RouteConfig.cs in App_Start and Add the namespace as shown below:
    C#
    public static void RegisterRoutes(RouteCollection routes)
    {
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
      routes.MapRoute(
          name: "Default",
          url: "{controller}/{action}/{id}",
          defaults: new { controller = "Home", 
          action = "Index", id = UrlParameter.Optional },
          <font style="background-color: #ffff00">namespaces: new string[] { "MainApplication.Controllers" }</font>
      );
    }
  15. Similarly go to BlogAreaRegistration.cs in Areas > Blog and add namespace as shown below:
    C#
    public override void RegisterArea(AreaRegistrationContext context)
    {
      context.MapRoute(
          "Blog_default",
          "Blog/{controller}/{action}/{id}",
          new { action = "Index", id = UrlParameter.Optional },
          <font style="background-color: #ffff00">new string[] { "MainApplication.Areas.Blog.Controllers" }
    </font>  );
    }
  16. By adding namespace to both RouteConfig and BlogAreaRegistration, the error will get fixed, now run the application again and you should see the below output.

    image

  17. Now try to navigate to Blog Home by typing http://localhost:<port>/blog/home in the address bar of the browser.

    image

  18. For navigating from Main Application to Blog Home, add the below ActionLink in the Main Application > Views > Home > Index.
    JavaScript
    @Html.ActionLink("Blog Home", "Index", "Home", new { area = "Blog" }, null)
  19. The above line should render a hyperlink as shown below, by clicking on it will navigate to Blog/Home.

    image

  20. Similarly for navigating from Blog Home to Main Application, add the below ActionLink in the Main Application > Areas > Blog > Views > Home > Index.
    HTML
    @Html.ActionLink("Home", "Index", "Home", new { area = "" }, null)
  21. The above line should render a hyperlink as shown below, by clicking on it will navigate to Main Application Home.

    image

You can get the source code from https://github.com/arunendapally/BasicMVCArea.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Architect Thomson Reuters
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionimages? Pin
Ian Klek17-Oct-16 4:03
Ian Klek17-Oct-16 4:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.