我在
下有一个布局页面〜/地区/管理员/共享/ _Layout.cshtml
现在在里面,我有一个部分,我应该渲染局部视图。所以我在_layout.cshtml
内做的是提供@Html.RenderAction("Sidebar")
控制器实际上是basecontroller,它继承给所有其他控制器。如
[OutputCache(Duration=60)]
public partial class BaseController : Controller
{
[ChildActionOnly]
public virtual ActionResult Sidebar()
{
return View();
}
}
现在这个控制器应该被所有的X,Y,Z控制器所占用,所以所有这些控制器都可以使用子进程Sidebar
,这样@Html.Renderaction("Sidebar")
就不会有找到子动作的麻烦被渲染。
现在问题是部分视图路径位于/Areas/Admin/Views/Shared/Partials/Sidebar/cshtml
我还配置了剃刀视图引擎,以便在该特定/Areas/Admin/Views/Shared/Partials/Sidebar.cshtml
下找到。并在global.asax下注册。
但它无法找到局部视图并将错误视为
~/Areas/Admin/Views/Admin/Sidebar.aspx
~/Areas/Admin/Views/Admin/Sidebar.ascx
~/Areas/Admin/Views/Shared/Sidebar.aspx
~/Areas/Admin/Views/Shared/Sidebar.ascx
~/Views/Admin/Sidebar.aspx
~/Views/Admin/Sidebar.ascx
~/Views/Shared/Sidebar.aspx
~/Views/Shared/Sidebar.ascx
~/Areas/Admin/Views/Admin/Sidebar.cshtml
~/Areas/Admin/Views/Admin/Sidebar.vbhtml
~/Areas/Admin/Views/Shared/Sidebar.cshtml
~/Areas/Admin/Views/Shared/Sidebar.vbhtml
~/Admin/Sidebar.cshtml
~/Views/Admin/Sidebar.vbhtml
~/Views/Shared/Sidebar.cshtml
~/Views/Shared/Sidebar.vbhtml
我的自定义剃刀视图引擎
public class LocalizedViewEngine : RazorViewEngine
{
///{0} = View Name
///{1} = Controller Name
private static readonly string[] NewPartialViewFormats = new[] {
"~/Areas/Admin/Views/{1}/Partials/{0}.cshtml",
"~/Areas/Admin/Views/Shared/Partials/{0}.cshtml",
"~/Views/Shared/Partials/{0}.cshtml",
"~/Views/{1}/Partials/{0}.cshtml"
};
private static readonly string[] NewViewLocationFormats = new[] {
"~/Areas/Admin/Views/{1}/{0}.cshtml"
};
public LocalizedViewEngine()
{
base.ViewLocationFormats =
base.ViewLocationFormats.Union(NewViewLocationFormats).ToArray<string>();
base.PartialViewLocationFormats =
base.PartialViewLocationFormats.Union(NewPartialViewFormats).ToArray<string>();
}
}
我的global.asax包含
ViewEngines.Engines.Add(new LocalizedViewEngine());
但它无法找到tha ~/Areas/Admin/Views/Shared/Partials/Sidebar.cshtml
下的部分视图。我哪里错了?