asp.net-mvc – 如何为指定的控制器分组部分共享视图?

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

我正在使用WebFormViewEngine.

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

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

最佳答案 当然.在这种情况下不要更改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;  
    }
点赞