ControllerC / ActionA的标准MVC模式,当控制器代码只是状态返回时View()是寻找ControllerC / ActionA.cshtml。我想这样做,如果这样的视图不存在,它会寻找一些默认视图,如Shared / Default.cshtml。我怎么能这样做?
答案 0 :(得分:1)
我不了解MVC 5,但您可以从RazorViewEngine创建自定义类。
public class MyFallbackLocationViewEngine : RazorViewEngine
{
public MyFallbackLocationViewEngine()
{
// Keep default locations and add our own fallback view
List<string> newLocations = new List<string>(ViewLocationFormats);
newLocations.Add("~/Views/Shared/Default.cshtml");
this.ViewLocationFormats = newLocations.ToArray();
}
}
并将其添加到您的Application_Start:
// Clear default engines and add only yours
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new MyFallbackLocationViewEngine());
您可以自定义您的ViewEngine,只需覆盖并覆盖其他方法