ASP.NET Response Redirect, Server.TransferUse Redirect and RedirectPermanent to redirect on the client-side. And use Server.Transfer for a server redirect.
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, and helps users too.
Notes, methods. ASP.NET provides Redirect and RedirectPermanent for this requirement. And Server.Transfer performs a redirection on the server itself—so the client never knows the redirect occurred.
Redirect example. We use the Redirect method. 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.
Example that uses Redirect: C#
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>
Notes, page. The page, when accessed, returns a HTTP 302 Found header. The location is set to the first argument of the Redirect call. The body of the response contains an HTML page, which won't typically be rendered by browsers.
Note When you call Redirect, browsers and search engine spiders alike will be directed to the new page.
Method that calls RedirectPermanent: C#
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>
RedirectPermanent. RedirectPermanent returns a 301 HTTP response. It redirects permanently to another location. Search engines (such as Google and Bing) 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.
Example method: C#
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; } }
Notes, output. What does the RedirectPermanent end up sending to the browser? It sends a HTTP header with the Location key, and an HTML page with a clickable link on it. In the browsers, the HTML will never be seen.
Instead The browser immediately shows the URL referenced in the Location header. The HTTP code is 301: "Moved Permanently."
Notes, search engines. The reason you don't just use Redirect is that search engines will continue to think that the original URL is the one to keep in their index. Users will experience worse performance.
Tip The RedirectPermanent is a useful addition to the .NET Framework version 4.0. It is built into the Framework.
Server.Transfer. Server.Transfer changes the page being rendered. This happens all on the server. A redirect is not issued to the browser. In this OnLoad example we use Server.Transfer to return a 404 error.
Note Here is a way to redirect to a 404 page in your ASP.NET project when an error occurs.
Transfer 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.
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.
© 2007-2021 sam allen. see site info on the changelog