TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

ASP.NET Server.Transfer Method

This C# article shows how to use Server.Transfer in ASP.NET. Server.Transfer has benefits.

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.

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.

TryCatch

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.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf