有没有人知道怎么告诉子控制器加载基(父)控制器视图而不是在它自己的文件夹中查找它?
public class BaseController : Controller
{
public virtual ActionResult Test()
{
return View("Test");
}
}
public class ChildController:BaseController
{
public override ActionResult Test()
{
return base.Test();
}
错误是视图'测试'找不到或没有视图引擎支持搜索的位置。搜索了以下位置: 〜/查看/儿童/ Test.cshtml .....
所以我找到的解决方案就是让父动作看起来像这样
public class BaseController : Controller
{
public virtual ActionResult Test()
{
return View("~Views/Base/Test.cshtml");
}
}
有没有更好的解决方案?
答案 0 :(得分:0)
private const string ViewPath = "~/Views/{0}/{1}.cshtml";
private ViewResult GetView(string action, object model = null)
{
var controllerName = ControllerContext.RouteData.Values["controller"].ToString();
var path = string.Format(ViewPath, controllerName, action);
return
new ViewResult
{
ViewName = path
};
}