【发布时间】:2016-06-01 17:01:19
【问题描述】:
我遇到了一个奇怪的问题,我不能 100% 确定有什么方法可以解决这个问题。
给定一个 MVC 控制器,BaseController(下面的最小示例)
BaseController : Controller
{
public ActionResult LotsOfActions()
{
//this calls a view that renderActions AnAction(SomeModel) a bunch of times.
}
public ActionResult AnAction(Object SomeModel)
{
//do stuff
return View("AnAction",TemplateFromCode,ViewModel); //Depending on something in SomeModel, we want a different template for this.
}
}
DerivedController : BaseController
{
public override ActionResult LotsOfActions()
{
//this calls a view that renderActions BaseController.AnAction(SomeModel) a bunch of times but with different logic.
}
}
当在 DerivedController 上调用 AnAction 时,它会尝试使用 DerivedController/AnAction.cshtml(或任何名称),我不需要存在它,因为子视图应该相同,所以我得到一个未找到视图的错误.我如何让它按照我的意图使用 BaseController/AnAction.cshtml?我不想为此使用共享视图文件夹,因为这是在 DerivedController 之前扫描的,以防我确实想为其他作为 BaseController 子类的东西覆盖此视图。
【问题讨论】:
-
你可以在你的基本控制器中明确表示:
return View("~/Views/Base/AnAction.cshtml"... -
这……实际上非常明显。谢谢。如果您将此作为答案删除,我会迅速将其标记为答案。以为以编程方式会更容易,但是嘿。
标签: c# asp.net asp.net-mvc-4 razor