0

我有一个 MVC4 应用程序,它当前使用一个区域(除了正常位置)。通常情况下,我的路由一切正常,但我现在需要在“根”位置使用代码来查找该区域内的局部视图。

我的解决方案文件的相关部分是:

/Areas
/Areas/Admin
/Areas/Admin/Views
/Areas/Admin/Views/Shared/_adminPartialView.cshtml
/Views
/Views/Shared/_rootPartialView.cshtml

我失败的代码是:

var viewEngine = new RazorViewEngine();
var cContext = new ControllerContext(context, new RouteData(), new EmptyController());

// this works:
var rootView = viewEngine.FindPartialView(cContext, "_rootPartialView", false);
if (rootView == null) { throw new Exception("Root view not found"); }

// this throws my error:
var adminView = viewEngine.FindPartialView(cContext, "_adminPartialView", false);
if (adminView == null) { throw new Exception("Admin view not found"); }

context以上间接来自 HttpContext.Current)

...这是有道理的,如果没有指定区域,则不应搜索管理区域...但是当我将viewEngine上面的内容更改为 = new AdminRazorViewEngine();时,定义为:

public class AdminRazorViewEngine : RazorViewEngine {
    public AdminRazorViewEngine() {
        var viewLocations = new [] {
            "~/Areas/Admin/Views/{1}/{0}.cshtml",
            "~/Areas/Admin/Views/Shared/{0}.cshtml"
        };
        base.ViewLocationFormats = viewLocations.Concat(base.ViewLocationFormats).ToArray();
    }
}   

我仍然得到相同的结果。

如果我复制_adminPartialView.cshtml/Views/Shared/,则使用任一视图引擎都可以正确找到两个视图。

我还尝试创建一个实际的 RouteData 对象,在其上指定区域,并在构造我的 ControllerContext 时使用它:

var routeData = new RouteData();
routeDate.Values.Add("area", "Admin");
// ...
var cContext = new ControllerContext(context, routeData, new EmptyController());

在行为没有改变的情况下。

如何明确指定视图引擎要搜索的区域?

4

1 回答 1

0

尝试设置AreaViewLocationFormats而不是ViewLocationFormats

这是来自 ILSpy 的代码RazorViewEngine

    public RazorViewEngine(IViewPageActivator viewPageActivator) : base(viewPageActivator)
    {
        base.AreaViewLocationFormats = new string[]
        {
            "~/Areas/{2}/Views/{1}/{0}.cshtml",
            "~/Areas/{2}/Views/{1}/{0}.vbhtml",
            "~/Areas/{2}/Views/Shared/{0}.cshtml",
            "~/Areas/{2}/Views/Shared/{0}.vbhtml"
        };
        base.AreaMasterLocationFormats = new string[]
        {
            "~/Areas/{2}/Views/{1}/{0}.cshtml",
            "~/Areas/{2}/Views/{1}/{0}.vbhtml",
            "~/Areas/{2}/Views/Shared/{0}.cshtml",
            "~/Areas/{2}/Views/Shared/{0}.vbhtml"
        };
        base.AreaPartialViewLocationFormats = new string[]
        {
            "~/Areas/{2}/Views/{1}/{0}.cshtml",
            "~/Areas/{2}/Views/{1}/{0}.vbhtml",
            "~/Areas/{2}/Views/Shared/{0}.cshtml",
            "~/Areas/{2}/Views/Shared/{0}.vbhtml"
        };
        base.ViewLocationFormats = new string[]
        {
            "~/Views/{1}/{0}.cshtml",
            "~/Views/{1}/{0}.vbhtml",
            "~/Views/Shared/{0}.cshtml",
            "~/Views/Shared/{0}.vbhtml"
        };
        base.MasterLocationFormats = new string[]
        {
            "~/Views/{1}/{0}.cshtml",
            "~/Views/{1}/{0}.vbhtml",
            "~/Views/Shared/{0}.cshtml",
            "~/Views/Shared/{0}.vbhtml"
        };
        base.PartialViewLocationFormats = new string[]
        {
            "~/Views/{1}/{0}.cshtml",
            "~/Views/{1}/{0}.vbhtml",
            "~/Views/Shared/{0}.cshtml",
            "~/Views/Shared/{0}.vbhtml"
        };
        base.FileExtensions = new string[]
        {
            "cshtml",
            "vbhtml"
        };
    }
于 2013-08-28T20:21:58.350 回答