如何对指定控制器的部分共享视图进行分组?

发布于 2024-08-03 16:08:46 字数 556 浏览 5 评论 0原文

是否可以告诉 ViewEngine 在指定控制器的其他文件夹中查找部分共享视图(而对于其他控制器则不能)?

我正在使用 WebFormViewEngine。

这就是我的 PartialViewLocations 目前的样子。

 public class ViewEngine : WebFormViewEngine
    {
        public ViewEngine()
        {
            PartialViewLocationFormats = PartialViewLocationFormats
                .Union(new[]
                       {
                           "~/Views/{1}/Partial/{0}.ascx",
                           "~/Views/Shared/Partial/{0}.ascx"
                       }).ToArray();
        }

Is it possible to tell ViewEngine to look for partial shared views in additional folders for specified controllers (while NOT for others)?

I'm using WebFormViewEngine.

This is how my PartialViewLocations looks at the moment.

 public class ViewEngine : WebFormViewEngine
    {
        public ViewEngine()
        {
            PartialViewLocationFormats = PartialViewLocationFormats
                .Union(new[]
                       {
                           "~/Views/{1}/Partial/{0}.ascx",
                           "~/Views/Shared/Partial/{0}.ascx"
                       }).ToArray();
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请远离我 2024-08-10 16:08:46

当然。在这种情况下,不要更改 PartialViewLocationFormats;相反,做:

    public override ViewEngineResult FindPartialView(
        ControllerContext controllerContext, 
        string partialViewName, 
        bool useCache)
    {
        ViewEngineResult result = null;

        if (controllerContext.Controller.GetType() == typeof(SpecialController))
        {
             result = base.FindPartialView(
                 controllerContext, "Partial/" + partialViewName, useCache);
        }

        //Fall back to default search path if no other view has been selected  
        if (result == null || result.View == null)
        {
            result = base.FindPartialView(
                controllerContext, partialViewName, useCache);
        }

        return result;  
    }

Sure. Don't change PartialViewLocationFormats in this case; instead, do:

    public override ViewEngineResult FindPartialView(
        ControllerContext controllerContext, 
        string partialViewName, 
        bool useCache)
    {
        ViewEngineResult result = null;

        if (controllerContext.Controller.GetType() == typeof(SpecialController))
        {
             result = base.FindPartialView(
                 controllerContext, "Partial/" + partialViewName, useCache);
        }

        //Fall back to default search path if no other view has been selected  
        if (result == null || result.View == null)
        {
            result = base.FindPartialView(
                controllerContext, partialViewName, useCache);
        }

        return result;  
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文