ASP.NET MVC 4 Areas in separate projects not working (view not found)

谁说我不能喝 提交于 2019-11-26 19:16:06

问题


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!


回答1:


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



来源:https://stackoverflow.com/questions/12911006/asp-net-mvc-4-areas-in-separate-projects-not-working-view-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!