0

在 Razor 中,我有一些位于"~/areas/shared/views/shared/_partialName.cshtml". 如果我只是@{ Html.RenderPartial("_partialName", myModel"); }在我看来,它找不到它。相反,我必须完全限定通往该部分的路径。

我可以告诉我的项目"~/areas/shared/views/shared/"在尝试查找部分时将文件夹包含在它爬取的文件夹列表中吗?通过这种方式,我可以将代码片段保留在我的视图中,以呈现部分漂亮和简短的内容。谢谢!

4

1 回答 1

0

RazorViewEngine 类包含具有模式的属性,用于查找各种视图文件 - 区域、部分、所有内容。您可以很容易地覆盖它,并在 Globals.asax 中告诉您的应用程序使用您的实现:

public class MyViewEngine : RazorViewEngine
{
    public MyViewEngine()
    {
        // Use the standard partials location in addition to this location
        // {0} is the partial view name, {1} is the controller name
        String[] morePartialLocations = new String[] {
            "~/areas/shared/views/shared/{0}.cshtml"
        };
        PartialViewLocationFormats =
            PartialViewLocationFormats.Concat(morePartialLocations).ToArray();
    }
}

// in Globals.asax

protected void Application_Start()
{
    ...
    ViewEngines.Engines.Clear();
    ViewEngines.Engines.Add(new MyViewEngine());
    ...
}

Note that since it sounds like you're working with Areas, there's another property that you can override called AreaViewLocationFormats which you can update in the same manner and insert "{2}" as the name of the area.

于 2012-04-30T15:25:57.623 回答