How to create ASP.NET MVC area as a plugin DLL?

2020-05-19 06:16发布

问题:

Here is what I want to achieve, I want to separate AREAs of ASP.NET MVC as pure single DLL.

  1. Blog.DLL
  2. Forums.DLL
  3. FAQ.DLL

Each of them are individual ASP.NET MVC Area, with its own default CSHTML or ASPX pages. Installing, migrating and maintaining lots of pages including resources, javascripts and so on are real pain for long run. As most of these will hardly change.

My final website will be like this.

\MvcApplication
   \bin
      \MvcApplication.bin
      \Blog.dll
      \Forums.dll
   \Controllers
      \..
   \Models
      \..
   \Views
      \..
   \Global.asax
   \Web.config

Without adding any thing, just dropping Blog.dll, my application should support /blog and all its pages. If I want to customize something, than I can add area, and add only cshtml pages..

\MvcApplication
   \bin
      \MvcApplication.bin
      \Blog.dll
      \Forums.dll
   \Areas
      \Blog
          \Views
              \Shared
                  \BlogLayout.cshtml <-- this will override the look
   \Controllers
      \..
   \Models
      \..
   \Views
      \..
   \Global.asax
   \Web.config

This will help in reusing ASP.NET Area Plugins, by simply dropping the dll in bin folder. However web.config may require some changes, but most likely we will save configure values in database and only thing needed will be "Entity Framework connection string" in web.config.

My challenges (Questions)

  1. Is it possible? It sure looks to me, but will there be any reflection/permission issues?
  2. How do I include cshtml/aspx views within one DLL? Probably compiled versions? I have seen couple of text template based View Engines on codeplex but I am little confused on how to actually use them.
  3. And how do I get ViewEngine to first check if physical directory file exists or not and then look into cshtml/aspx within the dll itself as resource file?

回答1:

You may take a look at the following article which illustrates how a custom VirtualPathProvider could be used in order to retrieve Razor views that are embedded into separate assemblies as resources.



回答2:

Maybe worth looking at portable areas from mvccontrib. I haven't used them since MVC 2, but found the following SO question by someone having some problems using them with MVC 3, with some possible solutions: ASP.NET MVC 3, Razor Views, and Portable Areas



回答3:

MvcCodeRouting supports what you want, plus many other cool stuff. Check out this post about ASP.NET MVC Plugins.