views:

164

answers:

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?

A: 

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
Hmm, is there perhaps a more straightforward way to make controls available in multiple sites?
borisCallens
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
perhaps svn:externals could help you?
Andrew Bullock
Care to elaborate?
borisCallens
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
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
borisCallens
@Andrew. This is what I did in the end btw
borisCallens
A: 

See Phil Haacks new post for an alternative idea

Andrew Bullock
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.
borisCallens
Its the virtualpathprovider business, thats what you need to override/implement to do what you want
Andrew Bullock
Hmm, seems the rendering is file based untill deep in the engine :S This sucks.
borisCallens