简体   繁体   English

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

[英]How to group partial shared views for specified controllers?

Is it possible to tell ViewEngine to look for partial shared views in additional folders for specified controllers (while NOT for others)? 是否可以告诉ViewEngine在其他文件夹中查找指定控制器的部分共享视图(对于其他控制器则不是 )?

I'm using WebFormViewEngine. 我正在使用WebFormViewEngine。

This is how my PartialViewLocations looks at the moment. 这就是我的PartialViewLocations目前的样子。

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

Sure. 当然。 Don't change PartialViewLocationFormats in this case; 在这种情况下,请勿更改PartialViewLocationFormats。 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;  
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM