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

后端 未结 2 1785
北海茫月
北海茫月 2020-12-04 15:58

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

相关标签:
2条回答
  • 2020-12-04 16:47

    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.

    0 讨论(0)
  • 2020-12-04 16:48

    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

    0 讨论(0)
提交回复
热议问题