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

时间:2009-08-28 08:10:39

标签: asp.net-mvc directory partial-views viewengine

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

我正在使用WebFormViewEngine。

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

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

1 个答案:

答案 0 :(得分:2)

不确定。在这种情况下不要更改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;  
    }