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

前端 未结 3 1541
北海茫月
北海茫月 2020-12-19 08:03

I am trying to use it for Login page.

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

But I get The Except

相关标签:
3条回答
  • 2020-12-19 08:24

    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.

    0 讨论(0)
  • 2020-12-19 08:25

    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();
    }
    
    0 讨论(0)
  • 2020-12-19 08:32

    You do this!

            return new MVCTransferResult(...);
    

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

    0 讨论(0)
提交回复
热议问题