ASP.NET - [removed]

后端 未结 3 1892
温柔的废话
温柔的废话 2021-02-07 10:06

How do I redirect permanently in ASP DOT NET? I\'d like to do a 301 redirect from one page on my site to another page.

相关标签:
3条回答
  • 2021-02-07 10:13

    If you want to always redirect from one URL to another you can use the IIS rewrite module.

    In you web.config file, add the following:

    <system.webServer>
      <rule name="Redirect Source to Destination" stopProcessing="true">
        <match url="/source.aspx" />
        <action type="Redirect" url="/destination.aspx" redirectType="Permanent" />
      </rule>
    </system.webServer>
    
    0 讨论(0)
  • 2021-02-07 10:27
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Response.StatusCode = 301;
        Response.StatusDescription = "Moved Permanently";
        Response.RedirectLocation = "AnotherPage.aspx";
        HttpContext.Current.ApplicationInstance.CompleteRequest();
    }
    

    And in 4.0, there's a simple HttpResponse.RedirectPermanent() method that does everything above for you:

    Response.RedirectPermanent("AnotherPage.aspx");
    
    0 讨论(0)
  • 2021-02-07 10:30

    ASP.NET 4.0 Beta 1 has a Response.RedirectPermanent() method for doing 301 redirects, e.g.

    Response.RedirectPermanent("AnotherPage.aspx");
    

    From the ASP.NET 4.0 and Visual Studio 2010 Web Development Beta 1 Overview white paper:

    It is common practice in Web applications to move pages and other content around over time, which can lead to an accumulation of stale links in search engines. In ASP.NET, developers have traditionally handled requests to old URLs by using by using the Response.Redirect method to forward a request to the new URL. However, the Redirect method issues an HTTP 302 Found (temporary redirect) response, which results in an extra HTTP round trip when users attempt to access the old URLs.

    ASP.NET 4.0 adds a new RedirectPermanent helper method that makes it easy to issue HTTP 301 Moved Permanently responses.

    0 讨论(0)
提交回复
热议问题