【发布时间】:2013-04-02 20:07:40
【问题描述】:
我在 MVC4 中有一个使用区域的站点。在某些区域(我们称之为区域),我有一个控制器(Controller),具有以下操作:
public ActionResult Index()
{
return View();
}
public ActionResult OtherAction()
{
return View("Index");
}
如果我像这样简单地重定向到 Area/Controller/OtherAction,这将非常有用:
return RedirectToAction("OtherAction", "Controller", new { area = "Area" });
但我需要 (check here why) 进行这样的重定向:
RouteData routeData = new RouteData();
routeData.Values.Add("area", "Area");
routeData.Values.Add("controller", "Controller");
routeData.Values.Add("action", "OtherAction");
ControllerController controller = new ControllerController();
controller.Execute(new RequestContext(new HttpContextWrapper(HttpContext.ApplicationInstance.Context), routeData));
在这种情况下它不起作用。在最后一行之后,OtherAction 方法被执行,然后在这段代码的最后一行,它抛出了这个异常:
未找到视图“索引”或其主视图或没有视图引擎 支持搜索的位置。以下位置是 搜索:
~/Views/Controller/Index.aspx
~/Views/Controller/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Controller/Index.cshtml
~/Views/Controller/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml
为什么会发生这种情况,我该如何解决?
【问题讨论】:
标签: redirect asp.net-mvc-4 asp.net-mvc-areas