Partager via


Redirecting in ASP.NET Using HttpResponse.Redirect

The HttpResponse.Redirect method of ASP.NET can be used to redirect requests from inside an ASP.NET application.

Example Code

The following example shows you how to use the Visual Basic .NET programming language to attempt to read a cookie, then redirect the page to a URL specified as redirect in the query string, adding a query string variable called AcceptsCookies to the URL. All ASP.NET code is inline, meaning that no controls are used.

This example is described in more detail in the article Basics of Cookies in ASP.NET, in the MSDN Library.

Sub Page_Load() 
    Dim redirect As String = Request.QueryString("redirect") 
    Dim acceptsCookies As String 
    ' Was the cookie accepted? 
    If Request.Cookies("TestCookie") Is Nothing Then 
        ' No cookie, so it must not have been accepted 
        acceptsCookies = 0 
    Else 
        acceptsCookies = 1 
        ' Delete test cookie 
        Response.Cookies("TestCookie").Expires = _ 
           DateTime.Now.AddDays(-1) 
    End If 
    Response.Redirect(redirect & "?AcceptsCookies=" & acceptsCookies, _ 
       True) 
End Sub