18

I have tried to create simple proof-of-concept ASP.NET MVC 4 web site using areas in separate projects.

I tried to following tutorials: http://bob.archer.net/content/aspnet-mvc3-areas-separate-projects (Application doesn't work in virtual directory... I use IIS). I hope there is better way than virtual directories.

Then I tried this tutorial: http://forums.asp.net/t/1483660.aspx/1 But there is no "AreasManifestDir" element in *.csproj of area project (and got error "The view 'Index' or its master was not found or no view engine supports the searched locations")

Is there still support in ASP.NET for MVC 4? Because I found this answer that it can be removed in future: What are the pros and cons of Areas implemented as single projects vs multiple projects in asp.net mvc

I haven't found any how-to for MVC4.

Structure of solution is simple:

Solution 'MvcAreasMultiProject'
    Areas [Directory]
        Admin [Project]
        Models
        Views
        Controllers
        Routes.cs [Examples]
    MvcAreasMultiProject [MainProject]
        - References Admin project
        M.V.C

Routes.cs of Admin project:

namespace Admin
{
public class Routes : AreaRegistration
{
    public override string AreaName { get { return "Admin"; } }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Admin_Default",
            "Admin/{action}/{id}",
            new { controller = "Admin", action = "Index", id = "" },
            new[] { "Admin.Controllers" }
        );
    }
}
}

Thanks for any help!

Community
  • 1
  • 1
Tomino
  • 5,050
  • 6
  • 32
  • 47

2 Answers2

28

You could use the RazorGenerator package to embed your Razor views into a separate assembly. Here are the steps to make this work:

  1. Install the Razor Generator Visual Studio extension (Tools -> Extensions and Updates...)
  2. Create a new ASP.NET MVC 4 application using the empty template.
  3. Add a new class library project to the solution called AreasLibrary (you could also use an ASP.NET MVC project template in order to get Intellisense in Razor views)
  4. Install the RazorGenerator.Mvc NuGet to the AreasLibrary project.
  5. Add a controller inside the AreasLibrary project (~/Areas/Admin/Controllers/HomeController.cs):

    public class HomeController: Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    }
    
  6. Add a corresponding view (~/Areas/Admin/Views/Home/Index.cshtml):

    @* Generator: MvcView *@
    
    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>View1</title>
    </head>
    <body>
        <div>
            Index view        
        </div>
    </body>
    </html>
    
  7. In the properties of the view set the Custom Tool to RazorGenerator.

  8. Inside the class library add an ~/Areas/AdminAreaRegistration.cs:

    public class AdminAreaRegistration : AreaRegistration
    {
        public override string AreaName { get { return "Admin"; } }
    
        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Admin_Default",
                "Admin/{action}/{id}",
                new { controller = "Home", action = "Index", id = "" }
            );
        }
    }
    
  9. All that's left is to reference the class library in the main MVC application.

Reference: http://blog.davidebbo.com/2011/06/precompile-your-mvc-views-using.html

codingbiz
  • 25,120
  • 8
  • 53
  • 93
Darin Dimitrov
  • 960,118
  • 257
  • 3,196
  • 2,876
  • 1
    Maybe little bit different answer to my question, but right solution. I would like to make different project for each area. So steps 1-4 are correct. **Is not necessary to create one project with all areas, but is simpler to add models+views+controllers into separated projects directly**. Thanks for your help - using RazorGenerator is the solution. – Tomino Oct 16 '12 at 12:38
  • No, the areas are not necessary, but any MVC dev should understand Areas and how to use them to better organize things – xximjasonxx Mar 26 '13 at 16:26
  • @darin-dimitrov - Suppose I did not want to add the class library reference to the main MVC application as you've described in step 9 above but instead dynamically "discover" my MVC area class libraries. Is there someway to accomplish this? I've tried using Assembly.LoadFrom() to accomplish this, however I never seem to be able to find the view. Thanks! – Rockdocta Apr 25 '13 at 19:37
  • I've some what similar problem, but above solution not working in my case. Here is my question: http://stackoverflow.com/questions/23403227/how-to-deploy-asp-net-mvc-4-application-having-multiple-projects-in-area – Saurabh Palatkar May 01 '14 at 08:06
  • @Rockdosta - You may find this [blog post](http://www.paraesthesia.com/archive/2013/01/21/putting-controllers-in-plugin-assemblies-for-asp-net-mvc.aspx) useful. – ChristoD Oct 23 '14 at 12:18
  • Any ideas on how to get this area inside the layout of the main project? – Svish Oct 23 '14 at 14:27
  • Here is another detailed and updated reference for anyone trying to use RazorGenerator: https://www.codeproject.com/Articles/1169354/Pre-compiled-Razor-View-in-ASP-NET-MVC. This and the answer were very useful for me – piraces Mar 02 '17 at 15:02
0

In my case, I had done all but step #9 of Darin's suggestions above:

All that's left is to reference the class library in the main MVC application.

The solution did not require a reference to compile, so I overlooked it. At runtime though, the system failed to properly route the requests. Just a heads up in case someone else overlooks this minor point.

Andrew Dunaway
  • 1,186
  • 2
  • 15
  • 27