开发者

Sharing views, logic, etc. between MVC apps

开发者 https://www.devze.com 2023-03-12 11:34 出处:网络
We have an existing, fully functional, ASP.NET MVC 2 app. I am creating an MVC 3 app that is supposed to have the same look and feel as the existing one. That is, it should use the same navigation asc

We have an existing, fully functional, ASP.NET MVC 2 app. I am creating an MVC 3 app that is supposed to have the same look and feel as the existing one. That is, it should use the same navigation ascx, header and footer ascx, etc. The existing app's ascx's populate themselves based on what's in the model, so it's not "just" the UI stuff that I want to bring over, it's the models and controller logic pertaining to those controls too.

Is there开发者_如何转开发 any nice way to do this without majorly refactoring the original app? I've heard about Portable Areas (http://lostechies.com/erichexter/2009/11/01/asp-net-mvc-portable-areas-via-mvccontrib/), but it looks like that would require a significant refactor on the existing app. I've also looked into the Razor Single File Generator extension, which would allow us to compile views into dlls and share them, but, that would require converting the existing app to MVC 3, then the views to Razor. (And I'm still not sure it would be necessarily "easy" after that.)

Am I overlooking anything obvious here? It just feels like there should be a good way to do this, but perhaps the existing app just needs a big overhaul. :P

Thanks in advance!


The easiest solution is probably to update to MVC3 (trivial if you aren't integrating with something that can't use .NET 4) and convert views to Razor as you move views to a shared library. The Razor view engine internally has a number of differences from the web forms view engine that make it easier to adapt to use in shared libraries.

In my own projects I have the precompiled view engine handling all views, but it is easy enough to set up multiple view engines so that the first one to find a valid view will be used. For layouts you will need to keep a version for both view engines, but that isn't much additional effort if you have problems with converting all the views to Razor at once.

Shared controller logic is fairly easy. Define the controller in your shared library and subclass it in the controllers folder where the system will be looking for controllers.


You might also look into using this handy tool that converts MVC2 projects into MVC3 projects. I used it, and it worked pretty well with only a few minor adjustments after conversion.

http://blogs.msdn.com/b/marcinon/archive/2011/01/13/mvc-3-project-upgrade-tool.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号