如何在asp.net MVC中使用Server.Transfer方法?

Vik*_*kas 6 asp.net-mvc asp.net-mvc-routing

我正在尝试将其用于登录页面.

if (Session["UserID"] == null)
     Server.Transfer("/Account/Login", true);
Run Code Online (Sandbox Code Playgroud)

但是我得到了异常 - >执行子请求/帐户/登录时出错.

Sim*_*ver 10

你做这个!

        return new MVCTransferResult(...);
Run Code Online (Sandbox Code Playgroud)

请查看我的答案(链接)以及接受的答案.


Mar*_*son 9

要使用服务器传输方法,您可以从Simon Weaver 查看信息,但在您的问题的上下文中,我将使用重定向操作.

RedirectToAction(new {
   controller="Account", 
   action="Login"
});
Run Code Online (Sandbox Code Playgroud)

让它告诉登录控制器在哪里回去试试

RedirectToAction( new {
   controller="Account",
   action="Login",
   new RouteValueDictionary { 
      {"actionToGoBackTo", "theActionName"},
      {"controllerToGoBackTo", "theControllerName"}
   }); 
Run Code Online (Sandbox Code Playgroud)

请注意,Login操作需要使用两个字符串参数,actionToGoBackTo和controllerToGoBackTo.


小智 5

您应该获得与Server.Transfer中所需的完全相同的结果.

public ActionResult Index() {
    ......
      var url = "/MyContoller?param=xxx";
      Server.TransferRequest(url, true);
      return new EmptyResult();
}
Run Code Online (Sandbox Code Playgroud)