1

我有一个布局页面

~/Areas/Admin/Shared/_Layout.cshtml

现在在里面我有一个部分,我应该渲染一个局部视图。所以我在里面做_layout.cshtml的是提供@Html.RenderAction("Sidebar")

控制器实际上是继承给所有其他控制器的基本控制器。作为

[OutputCache(Duration=60)]
public partial class BaseController : Controller
{       
    [ChildActionOnly]
    public virtual ActionResult Sidebar()
    {

        return View();
    }
}

现在这个 Controller 应该被所有 X,Y,Z 控制器调用,所以子动作对Sidebar所有这些控制器都是可用的,这样就不用@Html.Renderaction("Sidebar")麻烦找到要渲染的子动作了。

现在的问题是部分视图路径在/Areas/Admin/Views/Shared/Partials/Sidebar/cshtml

我还配置了 razor 视图引擎以在特定/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。我哪里错了?

4

0 回答 0