HttpListenerResponse.Redirect(String) 方法

定義

將回應設定為重新導向用戶端至指定的 URL。

public:
 void Redirect(System::String ^ url);
public void Redirect (string url);
member this.Redirect : string -> unit
Public Sub Redirect (url As String)

參數

url
String

用戶端應用來尋找所要求之資源的 URL。

範例

下列程式代碼範例示範如何呼叫此方法。

public static void PermanentRedirect(HttpListenerRequest request, HttpListenerResponse response)
{
    if (request.Url.OriginalString == @"http://www.contoso.com/index.html")
    {
        // Sets the location header, status code and status description.
        response.Redirect(@"http://www.contoso.com/indexServer/index.html");
    }
}
Public Shared Sub PermanentRedirect(ByVal request As HttpListenerRequest, ByVal response As HttpListenerResponse)
    If request.Url.OriginalString = "http://www.contoso.com/index.html" Then
        ' Sets the location header, status code and status description.
        response.Redirect("http://www.contoso.com/indexServer/index.html")
    End If
End Sub

備註

方法 Redirect 可用來將用戶端重新導向至資源的新位置。 這個方法會將回應的 Location 標頭設定為 url、將 StatusCode 屬性 Redirect設定為 ,並將 StatusDescription 屬性設定為 「Found」。。

適用於

另請參閱