301错误和样本测试

时间:2011-07-11 10:08:50

标签: c# asp.net error-handling

如何在asp.net中测试本地主机中的301错误。我添加了global.asax页面并编写了代码

 void Application_BeginRequest(object sender, EventArgs e)
     {
         const string www = "http://www.localhost/test";
         const string redirect = "http://localhost/test/";
         string request = HttpContext.Current.Request.Url.ToString();

         if (request.StartsWith(www, StringComparison.InvariantCultureIgnoreCase))
         {
             HttpContext.Current.Response.Status = "301 Moved Permanently";
             HttpContext.Current.Response.AddHeader("Location",
               redirect + request.Substring(www.Length));
         }
     }

并通过虚拟目录配置localhost,但我无法看到任何更改作为我的配置。

1 个答案:

答案 0 :(得分:1)

为什么不使用HttpResponse.RedirectPermanent()?它内置于:

HttpContext.Current.Response.RedirectPermanent(redirectUrl);