views:

1439

answers:

2

This question has two parts really so even if you can help with just one part that will help me out a bit!

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 IE7 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)
+2  A: 

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!

Mauro
+5  A: 

There is a new redirect mode in asp.net 3.5 sp1 that you can now use so it doesnt 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. "

http://msdn.microsoft.com/en-us/library/system.web.configuration.customerrorssection.redirectmode.aspx

mattlant
Thanks but we are using .Net 2.0 in this instance, but I will keep the .net 3.5 sp1 info to mind.
Mauro
Shoot i am really sorry, i must have forgot the 2.0 part of your post by the time i got to finish reading it. My bad.
mattlant
No problem, I can use this as an argument for using 3.5 in future though :)
Mauro
upvoted as this is still very helpful, and it's good to be clear that 2.0 is - bluntly - broken in this area
annakata