Home
C#.ASP.NET
Response Redirect, Server.Transfer
This page was last reviewed on Sep 26, 2022.
Dot Net Perls
Redirect. A web page sometimes changes locations. In ASP.NET, it is important to redirect the old address to the new one. This makes links pointing to the old address still work.
Notes, methods. ASP.NET provides Redirect and RedirectPermanent. And Server.Transfer performs a redirection on the server itself—so the client never knows the redirect occurred.
Redirect example. First, we get the Response object from the base class. Then, we invoke the Redirect method, with a complete URI string specified as the first argument.
Info The second argument to Redirect is false. This indicates we do not want an exception to be thrown to terminate the page.
Warning If you do not pass false, an exception will occur and this can slow down websites and complicate error handling.
using System; using System.Web.UI; namespace WebApplication1 { public partial class List : Page { protected void Page_Load(object sender, EventArgs e) { // Get response. var response = base.Response; // Redirect temporarily. // ... Don't throw an HttpException to terminate. response.Redirect("http://www.dotnetperls.com/list", false); } } }
HTTP/1.1 302 Found Content-Type: text/html; charset=utf-8 Location: http://www.dotnetperls.com/list Server: Microsoft-IIS/7.0 Date: Fri, 13 Aug 2010 21:18:34 GMT Content-Length: 144 <html><head><title>Object moved</title></head><body> <h2>Object moved to <a href="http://www.dotnetperls.com/list">here</a>.</h2> </body></html>
RedirectPermanent. This returns a 301 HTTP response. It redirects permanently to another location. Search engines will change their indexes to point to the new page directly.
Tip To call RedirectPermanent you will need to get the Response object from the HttpContext.
Then You can call RedirectPermanent: if you pass false as the second parameter, you can perform further actions and avoid an exception.
using System; using System.Web.UI; namespace WebApplication1 { public partial class List : Page { protected void Page_Load(object sender, EventArgs e) { // Get response. var response = base.Context.Response; // Redirect permanently. response.RedirectPermanent("http://www.dotnetperls.com/list", false); } } }
HTTP/1.1 301 Moved Permanently Date: Fri, 13 Aug 2010 16:10:05 GMT Server: Microsoft-IIS/6.0 Location: http://www.dotnetperls.com/list Content-Type: text/html; charset=utf-8 Content-Length: 144 <html><head><title>Object moved</title></head><body> <h2>Object moved to <a href="http://www.dotnetperls.com/list">here</a>.</h2> </body></html>
Server.Transfer. This changes the page being rendered. A redirect is not issued to the browser. In this OnLoad example we use Server.Transfer to return a 404 error.
Detail This new method is an alternative to using the Response.Redirect method to transfer to another page.
Also The transfer takes place on the server instead of forcing the browser to redirect to a new page.
Tip Server.Transfer results in faster response times (no redirects). This saves bandwidth and can be helpful in some situations.
protected override void OnLoad(EventArgs e) { string query = Request.QueryString["id"]; // For example. ISitePage sitePage; try { sitePage = new SitePageContent(this, query); } catch { // Hit when we don't have the key in the hash. Server.Transfer("~/404.aspx"); return; } }
Summary. We examined Redirect, RedirectPermanent, and Server.Transfer. These methods all change the response from the server. Avoiding redirects when possible is a good plan for performance.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 26, 2022 (edit).
Home
Changes
© 2007-2024 Sam Allen.