Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best approach for redirecting user in asp.net?

Tags:

c#

asp.net

I am new to asp.net development.I am currently working of asp.net web application. While resolving an issue came across exception "thread is being aborted". I went through several article to get deep understanding for the exception.

I read this great article on codeproject by Shivprasad koirala which explained me about the use of Server.Transfer and Response.Redirect.

Then I went to this post by Thomas Marquardt which made me conclude Respose.Redirect(url,false) but better to use

Server.ClearError();
Response.Redirect(url, false);
Context.ApplicationInstance.CompleteRequest();

But again this article by Imrambaloch says there is a security concern using Response.Redirect(url, false), and to instead use

Response.Redirect(url, false);
var context = HttpContext.Current;
if (context != null)
{
    context.ApplicationInstance.CompleteRequest();
}

Now in the end I am a bit confused as which is the recommended way to redirect.

My question: What is the best way to redirect user eliminating the security concerns and performance issues?

Is Server.Transfer really that bad as compared to Response.Redirect?

like image 742
niteshkodle Avatar asked May 20 '15 17:05

niteshkodle


3 Answers

Adding to @William's answer:

Server.Transfer method doesn't change the Url in the browser but redirect the user to the new page. It keep the information of previous page on server, While Response.Redirect completely redirect without keep the state of last page.

For performance, Response.Redirect have another argument endResponse(bool). Passing true in the second argument could make your application slow as it's not recommended to use EndResponse() even if you're doing it manually to finish the response. Rather use Context.ApplicationInstance.CompleteRequest() as suggested in blogs you listed.

like image 95
vendettamit Avatar answered Sep 28 '22 06:09

vendettamit


As you probably read on those posts you cited, with Response.Redirect the transfer is done by the browser while with Server.Transfer - it is done on the server.

Use Server.Transfer when you want to navigate to pages that are on the same server and use Response.Redirect when you want to navigate between pages that are on different servers / domains.

Server.Transfer is faster since there is one less round trip - conserves server resources.

Bottom line – it depends on your requirements.

like image 35
William Xifaras Avatar answered Sep 28 '22 06:09

William Xifaras


The short answer is, do what the first article says to do.

Though technically correct, the second article overlooks the most simple solution: avoid doing a Response.Redirect in Page_Load. If you have to pull Response.Redirect in Page_Load, just take the hit on the ThreadAbortException for that one or two instances rather than write a bunch of silly code in an HttpModule.

Generally speaking, you probably don't have too many good reasons to pull a Response.Redirect in a Page_Load anyway.

For example, if you're using ASP.NET Forms Authentication, it'll take care of "You don't belong here, go to this logon page buddy" stuff for you.

As a side note, if you have the option to do so, focus your attention on the ASP.NET MVC technology. It will get you better jobs and less frustration as your career progresses.

like image 30
ntcolonel Avatar answered Sep 28 '22 04:09

ntcolonel