views:

831

answers:

3

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.

+8  A: 

You don't. 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.

Mark Dickinson
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
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
You mean, I should pass the url as routerdata like return RedirectToAction("Login", "Account", this.Url); ?
Vikas
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
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
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
A: 

Try using Response.Redirect("/Account/Login", true);

you can find difference between Server.Transfer and Response.Redirect here

Searock
+2  A: 

You do this!

        return new MVCTransferResult(...);

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

Simon_Weaver