【问题标题】:Page not Redirecting from Application Error,页面未从应用程序错误重定向,
【发布时间】:2013-11-26 14:07:52
【问题描述】:

如果 HttpError 是 404,我正在尝试重定向页面 它在本地主机上运行完美,但在 Live 站点上却不是。

这是我的代码

void Application_Error(object sender, EventArgs e)
{
    // Code that runs when an unhandled error occurs
    HttpException serverError = Server.GetLastError() as HttpException;

    if (serverError != null)
    {
        int errorCode = serverError.GetHttpCode();

        if (errorCode == 404)
        {
            Server.ClearError();
            if (Request.Url.ToString().Contains("contact.php"))
            {
                Response.Redirect("contact-us.aspx");
            }
        }
    }
}

谢谢。请告知如何让它在现场工作,

【问题讨论】:

    标签: c# asp.net .net url-rewriting


    【解决方案1】:

    为什么不使用 web.config 而不是 global.asax 文件?

    <system.webServer>
              <httpErrors>
                <remove statusCode="404" subStatusCode="-1" />                
                <error statusCode="404" path="contact-us.aspx" responseMode="ExecuteURL" />                
              </httpErrors>
           </system.webServer>
    

    【讨论】:

    • 这仅适用于一页.. 我有超过 1 页要重定向
    【解决方案2】:

    试试 Response.Redirect("~/contact-us.aspx");

    【讨论】:

      【解决方案3】:

      我想通了。我必须在 web.config 中为我需要重定向的任何页面编写规则

      <system.webServer>
          <rewrite>
            <rules>
              <rule name="contact">
                <match url="^(contact).php$"/>
                <action type="Redirect" redirectType="Permanent" url="https://www.example.com/contact-us.aspx" />
              </rule>
          </rewrite>
      </system.webServer>
      

      这在我必须重定向的所有页面上都非常有效。并且在 global.asax 中没有添加任何代码

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-04-27
        • 2012-03-04
        • 2012-09-11
        • 2022-01-16
        • 2012-04-17
        • 2017-11-10
        • 1970-01-01
        • 2018-04-18
        相关资源
        最近更新 更多