i am going Nuts, i am using MVCContrib, to create pluggable site using Portable Areas, and everything is working well so far, except that when i started using MVC Templates, what is happening is if i put the The templates in the respective folder of the View it works, examples
HostApplication/Views/Home/DisplayTemplates/FirstName.cshtml
HostApplication/Areas/PortableArea_Blog/Views/Home/DisplayTemplates/Auther.cshtml
but what i want really is the ability to create common templates Set and utilize it from either Host Application or Portable Area, so to do that i created a new Portable Area Called DisplayTemplates(to utilize MVCContrib Ability to compile Views), here is the portable Area structure
DisplayTemplates
|-Views
|-CommentTemplate.cshtml
now in my host Application i have created a Test Model and added UIHint Attribute
public class HostModel
{
[UIHint("~/Areas/DisplayTemplates/Comment.cshtml")]
public string Name { get; set; }
}
but it is not working, so i thought it has something to do with Partial Views Location so i created a CustomView Engine to find Partial Views in that Location and registerd it in Global.asax, here is a short idea about so i wont bore you with full code
public class AreaViewEngine : RazorViewEngine
{
public AreaViewEngine()
{
// {0} = View name
// {1} = Controller name
// View locations
ViewLocationFormats = new[]
{
"~/Areas/DisplayTemplates/{0}.cshtml"
};
PartialViewLocationFormats = ViewLocationFormats;
AreaPartialViewLocationFormats = ViewLocationFormats;
}
protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
{
return new RazorView(controllerContext, partialPath, null, true, new[] { "cshtml" });
}
protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
{
return new RazorView(controllerContext, viewPath, masterPath, true, new[] { "cshtml" });
}
}
what is even more weird, is that it seems that that UIHint with Explicit location to Display Template, does not work, here is an example
public class HostModel
{
//this works
[UIHint("FirstName")]
//this does not work
[UIHint("~/Views/Home/DisplayTemplates/FirstName.cshtml")]
public string Name { get; set; }
}
and yes
FirstName.cshtml is in HostApplication/Views/Home/DisplayTemplates/FirstName.cshtml
again sorry for the long post, but i gave up on finding a solution, so any help would be totally appreciated.
I think you've had no answers, because there isn't one :-(
I've searched high and low the last few days trying to find a solution to this (since as you mentioned, it's had a lot of views). Unfortunately I can't find any way to override this.
I think you're stuck :(
Edit: Came across this post for reading views from a database instead of the disk:
ASP.NET MVC load Razor view from database
I wonder whether the DisplayTemplates are read this way. If so, you could try hijacking them and reading them from another location (instead of DB)?
Danny is correct. The Templates are found the same way that Partial Views are found.
By default the WebFormViewEngine and RazorViewEngine are going to search the following locations for a template.
For display templates:
For editor templates:
I think the name of the sub-directories (i.e., "DisplayTemplates" and "EditorTemplates") are hard-coded into MVC somewhere (I know it's open source and I could find it, but I'm not going to).
I think the easiest way to change the location somewhat is to override the ViewEngine. My custom ViewEngine is pretty complicated at this point, but I suspect you could get away with the following.
Let's say you want your templates to be in ~/Views/Templates.
Create a class that inherits from the view engine you're using now (probably WebFormViewEngine or RazorViewEngine). Add an empty constructor. It should looks like this:
Now, add the following lines to the Application_Start method of Global.asax.cs:
At this point, compile and run your application. Everything should be running exactly like it is running now. You're basically using the same view engine you were using before.
But now, we want to add a location to search when looking for PartialViews. This is simply done by adding to the PartialViewLocationFormats. So, now in the constructor of your custom view engine, you want to add to the base class' array like so:
A couple of notes about the above:
Good luck.
Pardon if I missed something above, but is there a reason that the override of
Html.DisplayFor
that allows the template to be specified won't work here? For example:Cheers,
Instead of creating a new ViewEngine you can easily modify the existing ones at runtime:
Place the above in an appropriate location (like area registration) and you'll be fine.
It might be a little late :) but this is now possible in MVC 4. The generic templates has to be placed in
~/Views/Shared/EditorTemplates
Source