Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 813
Vikas Avatar asked May 11 '09 08:05

Vikas


People also ask

When should I use Server transfer?

Use "Server. Transfer" when you want to navigate pages which reside on the same server, use "Response. Redirect" when you want to navigate between pages which resides on different server and domain.

What is the difference between Server transfer and response dot redirect?

The Response. Redirect method redirects a request to a new URL and specifies the new URL while the Server. Transfer method for the current request, terminates execution of the current page and starts execution of a new page using the specified URL path of the page.

What is the difference between serer transfer and Server execute?

Once code execution gets over, the control returns to the initial page, just after where it was called. However, in the case of Server. Transfer, it works very much the same, the difference being the execution stops at the new page itself (means the control isn't returned to the calling page).

Which method is used to redirect user from directly from Server?

Redirect() and Server. Transfer(). Transfers the page control to the other page, in other words it sends the request to the other page. Causes the client to navigate to the page you are redirecting to.


2 Answers

You do this!

        return new MVCTransferResult(...);

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

like image 124
Simon_Weaver Avatar answered Sep 20 '22 13:09

Simon_Weaver


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.

like image 36
Mark Dickinson Avatar answered Sep 16 '22 13:09

Mark Dickinson