Google 404 and .NET Custom Error Pages

好久不见. 提交于 2020-01-01 08:48:34

问题


I've got an ASP.NET 2.0 website with a custom 404 page. When content is not found the site serves the custom 404 page with a query string addition of aspxerrorpath=/mauro.aspx. The 404 page itself is served with an HTTP status of 200. To try to resolve this I've added

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.StatusCode = 404;
    }

I added the Google widget and have two issues with it. In Internet Explorer 7 it does not display where it should. If I add it to the content, I get an "unknown error" on char 79 line 226 or thereabouts; if I add it to the head section the search boxes appear above the content. In Firefox it works fine.

So my issues are:

  1. How do I make the widget appear inline?
  2. How do I make the error page render as a 404 with the original name and path of the file being requested so that when I request mauro.aspx I get the content for the 404 page, but with the URL of mauro.aspx? (I assume that I will have to do some URL rewriting in the begin_request global.asax file, but would like this confirmed before I do anything silly.)

回答1:


There is a new redirect mode in ASP.NET 3.5 SP1 that you can now use so it doesn't redirect. It shows the error page, but keeps the URL the same:

"Also nice for URL redirects. If you set the redirectMode on in web.config to "responseRewrite" you can avoid a redirect to a custom error page and leave the URL in the browser untouched."

  • CustomErrorsSection.RedirectMode Property (MSDN)



回答2:


I've handled the 404 by doing this in the global.asax file

protected void Application_BeginRequest(object sender, EventArgs e)
{
    string url = Request.RawUrl;
    if ((url.Contains(".aspx")) && (!System.IO.File.Exists(Server.MapPath(url))))
    {
        Server.Transfer("/Error/FileNotFound.aspx");
    }
}

Now, if anyone can help me with the google widget that would be great!



来源:https://stackoverflow.com/questions/152307/google-404-and-net-custom-error-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!