2017-04-06 72 views
0

谢谢!RedirectToAction不会工作,它运行但不路由到url

我有一个问题。

的RedirectToAction将无法正常工作,它运行但不路由到URL

运行它的编辑控制器首先

public ActionResult Edit(int? id) 
    { 
     CheckAccess(); 

     if (id == null) 
     { 
      return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
     } 

     ..... 
    } 

它将访问到的checkAccess()方法,在运行时 返回RedirectToAction(“Error”,“Index”);它运行到URL确定,但不是路线,然后返回到“编辑”控制器和运行下一个命令:“如果(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(); 
     } 
    } 
+2

'return RedirectToAction(“Index”,“Error”,new {id = StaffUserType});'是正确的用法。操作名称应该被称为第一个参数,然后是控制器名称。 –

+1

你的'if(id == null)'的意义是什么? - 你已经退出并永远不会到达那行代码。 –

回答

1

编辑()永远不会返回RedirectToAction结果,因为从的checkAccess()的返回值是不是捕获并返回。

您可以修改的checkAccess()返回一个布尔

public bool CheckAccess() 
{ 
    int StaffUserType = 5; 
    if (Session["StaffUserType"] != null) 
     StaffUserType = Convert.ToInt32(Session["StaffUserType"]); 

    if (StaffUserType == 5) 
    { 
     return false; 
    } 
    else 
    { 
     return true; 
    } 
} 

然后检查这一结果的编辑,如果返回RedirectToAction结果是错误的。

public ActionResult Edit(int? id) 
{ 
    if (!CheckAccess()) 
    { 
     return RedirectToAction("Index", "Error"); 
    } 

    ..... 
} 
+0

非常感谢你! –