的Razor视图引擎的位置以不兑现? [英] Razor view engine location order not honored?

查看:261
本文介绍了的Razor视图引擎的位置以不兑现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个RazorViewEngine子类来重新定义它寻找的看法。

I created a RazorViewEngine subclass to redefine where it looks for views.

我设置PartialViewLocationFormats字符串数组包含值

I set PartialViewLocationFormats string array to contain values

"~/Views/{1}/{0}.cshtml",
"~/Views/{1}/Partial/{0}.cshtml",
"~/Views/Shared/{0}.cshtml",

第一个和最后是否有默认。第二一个是我加的人,这样我就可以在仅由该控制器内的意见,而不是将其添加到查看/共享文件夹中使用的部分景色每一个查看/ ControllerName文件夹中的部分子文件夹

The first and last are there by default. The 2nd one is the one I added, so that I could have a "Partial" subfolder in each "Views/ControllerName" folder for partial views used only by views within that controller, rather than adding them to the "Views/Shared" folder.

我现在遇到的问题是,调用 @ Html.Partial(_ TopNavbar)是找到和渲染'_TopNavbar.cshtml文件,该文件在视图/共享文件夹中,而不是在我的仪表板的一个查看/仪表板/部分文件夹中。

The issue I'm encountering is that calling @Html.Partial("_TopNavbar") is finding and rendering the '_TopNavbar.cshtml" file that's in the "Views/Shared" folder, rather than in the one under my DashBoards's "Views/Dashboard/Partial" folder.

在换句话说,有两个不同的文件夹命名为_TopNavbar.cshtml两个不同的文件,而我期待它在一个位置上找到,因为它更具体和数组中第一次出现,但它不工作这样,由于某种原因,我不明白。

In other words, there are two different files named "_TopNavbar.cshtml" in two different folders, and I'm expecting it to be found in one location because it's more specific and occurs first in the array, but it's not working that way for some reason I don't understand.

该部分呼叫是里面的查看/仪表板/ Index.cshtml',而我直接前往访问它本地主机:端口/仪表板/索引,所以我敢肯定,控制器和行动是正确的。

The Partial call is inside "Views/Dashboard/Index.cshtml', and I'm accessing it by directly navigating to "localhost:port/Dashboard/Index", so I'm sure the controller and action are correct.

推荐答案

我一直在使用一个子类RazorViewEngine只查找C#的观点获得了成功。我扩展它在一个特定的控制器文件夹查找谐音。

I have had success using a subclassed RazorViewEngine that only looks for C# views. I extended it to look in a specific controller folder for partials.

使用 CustomRazorViewEngine 以下,并要求其在调用 HTML /首页/指数。部分(_ SomePartial)里面的观点会寻找谐音顺序是:

Using the CustomRazorViewEngine below and requesting /Home/Index which is calling Html.Partial("_SomePartial") inside it's view will look for partials in this order:


  1. /Views/Home/Partials/_SomePartial.cshtml

  2. /Views/Home/_SomePartial.cshtml

  3. /Views/Shared/_SomePartial.cshtml

  1. /Views/Home/Partials/_SomePartial.cshtml
  2. /Views/Home/_SomePartial.cshtml
  3. /Views/Shared/_SomePartial.cshtml

这的确是回到正确​​的顺序的部分意见。

This is indeed returning the the partial views in the correct order.

public class CustomRazorViewEngine : RazorViewEngine
{
    public CustomRazorViewEngine()
    {
        ViewLocationFormats = new[]
        {
            "~/Views/{1}/{0}.cshtml",
            "~/Views/Shared/{0}.cshtml"
        };

        MasterLocationFormats = ViewLocationFormats;

        PartialViewLocationFormats = new[]
        {
            "~/Views/{1}/Partials/{0}.cshtml",
            "~/Views/{1}/{0}.cshtml",
            "~/Views/Shared/{0}.cshtml"
        };
    }
}

请确保你只使用你的子类视图引擎通过清除所有现有和添加新的自定义视图引擎。

Make sure that you are only using your subclassed view engine by clearing all existing and adding your new custom view engine.

的Global.asax.cs

ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new CustomRazorViewEngine());

这篇关于的Razor视图引擎的位置以不兑现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆