How do I use Server.Transfer method in asp.net MVC?

拟墨画扇 提交于 2019-11-28 04:25:59

问题


I am trying to use it for Login page.

if (Session["UserID"] == null)
     Server.Transfer("/Account/Login", true);

But I get The Exception -> Error executing child request /Account/Login.


回答1:


To use a server transfer method you could look at this from Simon Weaver, but in the context of your question I would use a redirect action instead.

RedirectToAction(new {
   controller="Account", 
   action="Login"
});

to get it to tell the login controller where to go back to try

RedirectToAction( new {
   controller="Account",
   action="Login",
   new RouteValueDictionary { 
      {"actionToGoBackTo", "theActionName"},
      {"controllerToGoBackTo", "theControllerName"}
   }); 

note that the Login action will need to take two string arguments, actionToGoBackTo, and controllerToGoBackTo.




回答2:


You do this!

        return new MVCTransferResult(...);

Please see my answer (linked) as well as the accepted answer.




回答3:


You should get the exactly same result as what you want in Server.Transfer.

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


来源:https://stackoverflow.com/questions/847232/how-do-i-use-server-transfer-method-in-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!