简体   繁体   中英

How to redirect to an ASPX page using a C# Class?

Please can anyone suggest me a better way on How to redirect to an ASPX page using a C# Class? The class has a method

protected void redirectTo() { 

/*Code Here*/

    }

When this method is called, I need to redirect the user to another page, How should i fill this method?

From another .aspx page:

Response.Redirect("Url.aspx");

From a class:

HttpContext.Current.Response.Redirect("Url.aspx");

Have a look at How to: Redirect Users to Another Page

Also HttpResponse.Redirect Method

Redirects a client to a new URL. Specifies the new URL and whether execution of the current page should terminate.

You can use

Response.Redirect("Url.aspx");

Response.Redirect() will send you to a new page, update the address bar and add it to the Browser History, ie it causes additional roundtrips to the server on each request. It doesn't preserve Query String and Form Variables from the original request. Its a Response. Redirect simply sends a message down to the (HTTP 302) browser. Context.Items are lost when navigate to the new page.

or

Server.Transfer("Url.aspx");

Whereas, Server.Transfer happens without the browser knowing anything, the browser request a page, but the server returns the content of new redirected page. It transfers current page request to another .aspx page on the same server. Data can be persist across the pages using Context.Item collection, which is one of the best way to transfer data from one page to another keeping the page state alive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM