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.
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.
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.
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).
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.
You do this!
return new MVCTransferResult(...);
Please see my answer (linked) as well as the accepted answer.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With