C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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.
Example. Sometimes on a site it is best to assume things will go as planned, and handle "exceptional" problems with try and catch. Here is a way to redirect to a 404 page in your ASP.NET project when an error occurs.
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; } }
404 pages. To solve the 404 problem, we can simply use Server.Transfer() to our 404 page and then return from the OnLoad/Page_Load method. This saves a request from the browser, so the user waits less, and your server does less work.
How to Use the Server.Transfer Method. This new method is an alternative to using the Response.Redirect method to transfer to another page, and allows the transfer of the ASP built-in and Error objects to a second page.
Also: The transfer takes place on the server instead of forcing the browser to redirect to a new page.
How to Use the Server.Transfer Method
This code reduces work on your server—work that would be completely useless to your business. It results in faster response times as well. This saves bandwidth and can be helpful in some situations.
Summary. We looked at the Server.Transfer method. This method eliminates one request from the browser. Server.Transfer is useful in many other situations, but this is an adequate example of its use. The URL in the browser will remain the same.