Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET custom error page HTTP response

Why does asp.net go from doing normal 404 responses when there are no custom errors to responding with either 302 or 200 when you turn custom errors on?

So for example I've tried all of the following:

<customErrors defaultRedirect="Error.aspx" mode="Off">
    <error statusCode="404" redirect="Error.aspx?status=404" />
</customErrors>

and

<httpErrors>
    <remove statusCode="404" subStatusCode="-1" />
    <error statusCode="404" path="/error.aspx?status=404" responseMode="ExecuteURL" />
</httpErrors>

and

<httpErrors>
    <remove statusCode="404" subStatusCode="-1" />
    <error statusCode="404" path="/error.aspx?status=404" responseMode="Redirect" />
</httpErrors>

As far as I can tell the top one gives a 302 as it redirects to the error page, the second gives a 200 for some reason and the final one also give a 302. This seems rather silly as I still want a 404 (or whatever other error code) but to display a friendly message.

I realize I can send a different response back with my error page but that seems to give the 404 code to the error page and keeps the 302 on the original request.

Unless I'm missing something...

like image 601
radm4 Avatar asked Jun 12 '13 14:06

radm4


People also ask

How can show custom error page in asp net?

Steps for Custom Error PageAdd Web Form for custom error page. Set <customErrors> setting in Web. Config file of the application. Pass defaultRedirect and mode attributes in <customErrors>.


1 Answers

You are right, ASP.NET behaves a bit funky when customErrors are enabled. In short, you can set redirectMode="ResponseRewrite" for customErrors and specifically set the response code in your error.aspx page.

I've blogged the details here: Keep ASP.NET error pages out of search engines.

like image 63
klings Avatar answered Oct 20 '22 23:10

klings