我要使用Nuts,我正在使用MVCContrib,使用Portable Areas创建可插拔站点,到目前为止一切运行良好,除了当我开始使用MVC模板时,发生的事情是如果我将模板放在相应的文件夹中查看它的工作原理,例子
HostApplication/Views/Home/DisplayTemplates/FirstName.cshtml
HostApplication/Areas/PortableArea_Blog/Views/Home/DisplayTemplates/Auther.cshtml
Run Code Online (Sandbox Code Playgroud)
但我真正想要的是能够创建通用模板从主机应用程序或便携式区域设置和利用它,所以要做到这一点,我创建了一个新的便携式区域称为DisplayTemplates(利用MVCContrib能力编译视图),这里是便携式区域结构
DisplayTemplates
|-Views
|-CommentTemplate.cshtml
Run Code Online (Sandbox Code Playgroud)
现在在我的主机应用程序中我创建了一个测试模型并添加了UIHint属性
public class HostModel
{
[UIHint("~/Areas/DisplayTemplates/Comment.cshtml")]
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用,所以我认为它与部分视图位置有关,所以我创建了一个CustomView引擎来查找该位置的部分视图并在Global.asax中注册它,这里有一个简短的想法,所以我不会厌烦你完整的代码
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 …Run Code Online (Sandbox Code Playgroud)