【发布时间】:2017-04-06 01:33:18
【问题描述】:
谢谢!!
我有一个问题。
RedirectToAction 不起作用,它运行但不路由到 url
它首先运行编辑控制器
public ActionResult Edit(int? id)
{
CheckAccess();
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
.....
}
它将访问 CheckAccess() 方法,当它运行 return RedirectToAction("错误", "索引");它运行正常但没有路由到 url,然后返回“编辑”控制器并运行下一个命令“if (id==null)。
public ActionResult CheckAccess()
{
int StaffUserType = 5;
if (Session["StaffUserType"] != null)
StaffUserType = Convert.ToInt32(Session["StaffUserType"]);
if (StaffUserType == 5)
{
//return Json(Url.Action("Index", "Error"));
return RedirectToAction("Error", "Index");
//return View("ErrorController/Index");
}
else
return View();
}
}
【问题讨论】:
-
return RedirectToAction("Index", "Error", new { id = StaffUserType });是正确的用法。动作名称应作为第一个参数提及,然后是控制器名称。 -
你的
if (id == null)有什么意义 - 你已经退出了,永远不会到达那行代码。
标签: c# asp.net-mvc