Google 404和.NET自定义错误页面

时间:2022-10-09 19:17:08

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

我有一个带有自定义404页面的ASP.NET 2.0网站。当找不到内容时,该站点提供自定义404页面,其中添加了aspxerrorpath = / mauro.aspx的查询字符串。 404页面本身的HTTP状态为200.为了尝试解决这个问题,我已经添加了

    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.

我添加了Google小部件,并且有两个问题。在Internet Explorer 7中,它不显示应该显示的位置。如果我将它添加到内容中,我会在char 79行226或其附近出现“未知错误”;如果我将其添加到头部,则搜索框将显示在内容上方。在Firefox中它工作正常。

So my issues are:

所以我的问题是:

  1. How do I make the widget appear inline?
  2. 如何使小部件显示为内联?
  3. 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.)
  4. 如何使用原始名称和所请求文件的路径将错误页面呈现为404,以便在我请求mauro.aspx时获取404页面的内容,但是使用mauro.aspx的URL? (我假设我必须在begin_request global.asax文件中进行一些URL重写,但是在我做任何愚蠢的事情之前都要确认这一点。)

2 个解决方案

#1


10  

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:

ASP.NET 3.5 SP1中有一个新的重定向模式,您现在可以使用它,因此它不会重定向。它显示错误页面,但保持URL相同:

"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."

“对于URL重定向也很好。如果你将web.config中的redirectMode设置为”responseRewrite“,你可以避免重定向到自定义错误页面,并保持浏览器中的URL不变。”

#2


1  

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

我通过在global.asax文件中执行此操作来处理404

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!

现在,如果有人可以帮助我使用谷歌小部件,这将是伟大的!

#1


10  

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:

ASP.NET 3.5 SP1中有一个新的重定向模式,您现在可以使用它,因此它不会重定向。它显示错误页面,但保持URL相同:

"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."

“对于URL重定向也很好。如果你将web.config中的redirectMode设置为”responseRewrite“,你可以避免重定向到自定义错误页面,并保持浏览器中的URL不变。”

#2


1  

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

我通过在global.asax文件中执行此操作来处理404

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!

现在,如果有人可以帮助我使用谷歌小部件,这将是伟大的!