6

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.

Mark Dickinson
  • 6,573
  • 4
  • 29
  • 41
Vikas
  • 24,082
  • 37
  • 117
  • 159
  • 1
    +1'ed Mark's answer. I would use "return RedirectToAction("SomeOtherAction");". Is there some reason you don't want visitors to see the redirected URL? – Jarrett Meyer May 11 '09 at 09:10

3 Answers3

10

You do this!

        return new MVCTransferResult(...);

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

Community
  • 1
  • 1
Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
9

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.

Community
  • 1
  • 1
Mark Dickinson
  • 6,573
  • 4
  • 29
  • 41
  • well, actually I am trying to solve the problem -> http://stackoverflow.com/questions/846775/how-to-redirect-user-to-the-page-where-session-expires-after-logged-in – Vikas May 11 '09 at 08:55
  • Can't you pass some routedata when you redirect, this will tell the login page what paeg to redirect to on succesful login. That way though, you'll have to get your submit method to redirect to whatever page the routedata says. – Mark Dickinson May 11 '09 at 08:58
  • You mean, I should pass the url as routerdata like return RedirectToAction("Login", "Account", this.Url); ? – Vikas May 11 '09 at 09:04
  • Sorry for the delay. You can pass the controller and action name in a new routevaluedictionary and redirect to this, you don't need to deal with urls so much in mvc. – Mark Dickinson May 11 '09 at 09:44
  • 1
    Thanks for your interest. I am following as you directed but still one problem comes with this method i.e. Form data (using input type="submit"). How can preserve those data? – Vikas May 12 '09 at 05:11
  • When you submit (using the Html helper's submit button), the names of the form fields should correspond to argument names on the action you are calling. Having received them in the action you can easily assign them to the viewdata, or to the routevaluedictionary your redirect action is passing. – Mark Dickinson May 12 '09 at 08:07
  • 1
    While this answer is helpful to the OP'r, it is unhelpful for those of us who are trying to find the answer to the same question for our own purposes. RedirectToAction actually emits a 301/302 redirect and is not the answer to the original question. Server.Transfer silently processes an alternate handler without the HTTP chatter, which is important if you do NOT want the processed handler to go back up through the routing engine again. – Jon Davis Feb 23 '12 at 21:43
  • @stimpy77 - thanks for your feedback, answer amended, credit where it's due :) – Mark Dickinson Feb 24 '12 at 08:56
  • Just checked but the question never mentions concern about chatty http. Also a quick read reveals this interesting stuff about what a 301 would do as part of a login sequence http://stackoverflow.com/questions/1723487/response-redirect-http-status-code (not saying right or wrong, just interesting :)) – Mark Dickinson Feb 24 '12 at 09:16
5

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();
}
Jeff Guo
  • 51
  • 1
  • 1