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.