views:

173

answers:

3

Hi,

In my site, I have used IIS7's URL rewrite module to redirect URLs like http://mysite.com/File.aspx?Name=SomeName into http://mysite.com/SomeName.

It appears that IIS7 has created a corresponding rule check, so that any URL of the sort http://mysite.com/SomeURL is redirected to File.aspx. This is fine in most cases, when the URL is correctly rewritten.

The problem is that in some cases, the file no longer exists - http://mysite.com/SomeName2 will still get redirected to http://mysite.com/File.aspx?Name=SomeName2.

I want to show a custom 404 error page for this URL - how do I trigger the 404 error in global.asax (I have set application error logging and handling in global.asax)? The below code doesn't work.

Response.Status = "404 Not Found"
Response.AddHeader("Location", "http://mysite.com/Invalid-File.aspx?" & Request.QueryString.ToString)

It just shows the ugly default IIS 404 error page. Adding a customerror in web.config doesn't help.

Thanks for your help!

A: 

you could potentially use the Application_Error method and then do a response.redirect to the required page with the correct status code

PaulStack
Hi,Application error isn't triggering, since the page is being redirected to http://mysite.com/File.aspx?Name=Somename - IIS7 doesn't realize the page doesn't exist.
Wild Thing
+1  A: 

When setting a status code you need to prevent IIS taking over based on your new error code, make sure to set Response.TrySkipIisCustomErrors like this:

Response.TrySkipIisCustomErrors = true;
Response.StatusCode = (int)HttpStatusCode.NotFound;
Response.AddHeader("Location", "http://mysite.com/Invalid-File.aspx?" & Request.QueryString.ToString)
Nick Craver
For some reason the page isn't redirecting to Invalid-File.aspx - the File.aspx page continues to be displayed! Response.Redirect results in the redirected page having a 302 error :(
Wild Thing
This is weird - on using your code, the page contents displayed are from File.aspx, but the headers state the code as 404, and the location as Invalid-File.aspx!
Wild Thing
@Wild - What do you *want* the result to be? A 404 to the client shouldn't be happening if you're redirecting to actual content, that's what a 301 or a 302 is for. Are you trying to do something closer to a `Server.Transfer()` on the response?
Nick Craver
I want it to be a 404. The file in question doesn't exist.Edit: To clarify, your above code would work great - *if* the actual redirect to the custom 404 page had worked - it doesn't.
Wild Thing
Oh shit! Didn't notice the part about server.transfer until now! It works! Yay!
Wild Thing
@Wild - Try `HttpContext.Current.Server.Transfer("~/Invalid-File.aspx");` for the last line of the answer above, this will keep the URL the user has, but render the contents of your 404 page.
Nick Craver
Hi Nick, it works this way even without the httpcontext.current. The URL is the missing page, but the content is shown from the 404 page. The headers are 404 - perfect!
Wild Thing
+1  A: 

Did you try setting the 404 error page in the IIS admin equal to what you're specifying in the web.config? I wonder if .net isn't jumping into the pipeline at the right time.

ScottE
The custom 404 page is already present in IIS admin in the .Net error pages section for.
Wild Thing
So you changed the setting in IIS Manager > IIS > Error Pages? I'm not talking about the .Net Error Pages - this just changes your web.config.
ScottE
I see - can you please clarify what you meant?
Wild Thing
Make sure you have a 404 entry in IIS Manager > IIS > Error Pages that matches the 404 entry you have in IIS Manager > ASP.NET> .NET Error Pages
ScottE
Aah, I see - now it makes sense. In any case my global.asax App_Error is getting triggered before the IIS 404 error page can be displayed (I tried setting it where you have mentioned). To trigger a 404, I tried accessing doesntexist.aspx, which bypasses the URL rewrite (files with .aspx extensions are excluded).
Wild Thing