2

Is there a way to load partial views from a class library?
I would like to pull out the common parts from several websites to make them easier to maintain.

Currently I render my partials with (for example)

<%Html.RenderPartial("~/Views/Search/_searchBlock.ascx", ViewData);%>

This is file based, but when creating a class library, the views are compiled. Is there a way to load libraries that are compiled in this library?

Boris Callens
  • 90,659
  • 85
  • 207
  • 305
  • This seems like a very useful thing to do, especially when migrating an app from WebForms to MVC. Is it possible in more recent versions of asp.net mvc or mvc 2? – Ashley Tate May 11 '10 at 16:16
  • @Ashley. I have seen it used in a tutorial a while ago. But as I'm not working on that project anymore I didn't use it in practice. If I manage to find it again, I'll post a link later – Boris Callens May 12 '10 at 09:29

2 Answers2

0

The only way i can think to do this would be with embedded resources and a custom viewengine. Point reflector (or download the MVC soruce) at System.Web.Mvc.Html.RenderPartialExtensions

Andrew Bullock
  • 36,616
  • 34
  • 155
  • 231
  • Hmm, is there perhaps a more straightforward way to make controls available in multiple sites? – Boris Callens Apr 15 '09 at 10:26
  • Im not sure, I'd be interested if there is. It will be a bit of a job to do it my way, you'll need to replace the call to BuildManager.CreateInstanceFromVirtualPath with something that reads the textstream for you file from the assembly resources, compile it into a ViewUserControl. effort basically – Andrew Bullock Apr 15 '09 at 12:39
  • perhaps svn:externals could help you? – Andrew Bullock Apr 15 '09 at 13:09
  • http://bitworking.org/news/Sourceforge__Subversion_and_svn_externals You could put the shared .ascxs in their own repos and then use svn:externals to reference them from each project – Andrew Bullock Apr 15 '09 at 14:50
  • I fear it's not possible in my current company's setup (don't ask :S ), but it looks like a solution to me. Thx – Boris Callens Apr 16 '09 at 05:35
  • @Andrew. This is what I did in the end btw – Boris Callens May 12 '10 at 09:30
0

See Phil Haacks new post for an alternative idea

Andrew Bullock
  • 36,616
  • 34
  • 155
  • 231
  • He, I was reading it whilst you answered. Appreciate you turning back tho this. Although it does look wicked, I fail to see how this could be of much use in my scenario. I think I'm going to have a stab at creating my own method that locates views. That way I could for example add a appKey that says where they are located and all sites would go pick it up from there rather then searching in it's own folder. – Boris Callens Apr 23 '09 at 10:04
  • Its the virtualpathprovider business, thats what you need to override/implement to do what you want – Andrew Bullock Apr 23 '09 at 18:38
  • Hmm, seems the rendering is file based untill deep in the engine :S This sucks. – Boris Callens Apr 27 '09 at 13:44