Asp Net Error Page Status Code
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta asp net response status code Discuss the workings and policies of this site About Us Learn
Asp Net Error Page Web Config
more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us
Asp Net Statuscode
Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like
Asp.net Mvc Custom Error Page
you, helping each other. Join them; it only takes a minute: Sign up ASP.Net 4: Custom error pages with correct HTTP status code, how? up vote 2 down vote favorite 1 I'm running a ASP.Net 4 site on Windows 2008 R2 with IIS 7.5. When developing I use windows 7 and VS 2010 with IIS 7.5 as asp.net error handling well. I've setup my web.config as such: And my Error404.aspx page has this in its code-behind: protected void Page_Load(object sender, EventArgs e) { Response.StatusCode = 404; } On my developer machine this works fine, I see my pretty errorpage in the browser, and using Fiddler I can confirm I only receive the 404 HTTP status. Now when I publish it to my server, it's a different story. Here I'll see the IIS' own 404 page ("C:\inetpub\custerr\en-US\404.htm") If I remove the Response.StatusCode in my code-behind, I get the errorpage fine on the server, albeit with a 200 HTTP status. What am I overlooking since this doesn't work ? asp.net c#-4.0 iis-7.5 share|improve this question asked Mar 10 '11 at 8:56 Steffen 6,10833257 add a comment| 1 Answer 1 active oldest votes up vote 2 down vote accepted Does this article by Rick Strahl help? It covers 500 errors and the TrySkipIisCustomErrors property to override the default IIS behaviour. share|improve this answer
resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community Magazine Forums Blogs Channel 9 Documentation APIs and reference Dev asp.net custom error page centers Retired content Samples We’re sorry. The content you requested has been removed. httpstatuscode You’ll be auto redirected in 1 second. ASP.NET Configuration Settings TOC Collapse http status code the table of content Expand the table of content This documentation is archived and is not being maintained. This documentation is archived and is not being maintained. Recommended Version This documentation http://stackoverflow.com/questions/5257362/asp-net-4-custom-error-pages-with-correct-http-status-code-how is archived and is not being maintained. error Element for customErrors (ASP.NET Settings Schema) .NET Framework 3.0 Other Versions Visual Studio 2010 .NET Framework 4 Visual Studio 2008 .NET Framework 3.5 .NET Framework 2.0 .NET Framework 1.1 Specifies the custom error page for a given HTTP status code. Copy Attributes and Elements The following sections describe attributes, child elements, and parent https://msdn.microsoft.com/en-us/library/s2f4e3e7(v=vs.85).aspx elements. Attributes Attribute Description statusCode Required attribute. Specifies the HTTP status code that results in redirection to the error page. redirect Required attribute. The URL of the custom page that is mapped to the error code. Child Elements None. Parent Elements Element Description configuration Specifies the required root element in every configuration file that is used by the common language runtime and the .NET Framework applications. system.web Specifies the root element for the ASP.NET configuration section. customErrors Provides information about custom error messages for an ASP.NET application. It can be defined at any level in the application file hierarchy. Example The following configuration code example specifies the error handling pages to use for an ASP.NET application. The custom errors are only issued for the elements that are handled by ASP.NET. For example, if you refer to a non-existing .htm page, Microsoft Internet Information Services (IIS) issues the standard 404 error. If you refer instead to a non-existing .aspx page, ASP.NET issues the custom 404 error, if you configured the error. Copy Element Information Configuration section handler System.Web.Co
pages, chances are your site is returning the incorrect HTTP status codes for the errors that your users are experiencing (hopefully as few as possible!). Sure, your users see a pretty error page just fine, but your http://www.digitallycreated.net/Blog/57/getting-the-correct-http-status-codes-out-of-asp.net-custom-error-pages users aren’t always flesh and blood. Search engine crawlers are also your users (in a sense), and they don’t care about the pretty pictures and funny one-liners on your error pages; they care about the HTTP status codes returned. For example, if a request for a page that was removed consistently returns a 404 status code, a search engine will remove it from its index. However, if it doesn’t and instead returns the wrong error code, asp net the search engine may leave the page in its index. This is what happens if your non-existent pages don't return the correct status code! Unfortunately, ASP.NET custom error pages don’t return the correct error codes. Here’s your typical ASP.NET custom error page configuration that goes into the Web.config: And here’s a Fiddler trace of what happens when someone request a page that should asp net error simply return 404: A trace of a request that should have just 404'd As you can see, the request for /ThisPageDoesNotExist returned 302 and redirected the browser to the error page specified in the config (/Pages/Error404), but that page returned 200, which is the code for a successful request! Our human users wouldn’t notice a thing, as they’d see the error page displayed in their browser, but any search engine crawler would think that the page existed just fine because of the 200 status code! And this problem also occurs for other status codes, like 500 (Internal Server Error). Clearly, we have an issue here. The first thing we need to do is stop the error pages from returning 200 and instead return the correct HTTP status code. This is easy to do. In the case of the 404 Not Found page, we can simply add this line in the view: <% Response.StatusCode = (int)HttpStatusCode.NotFound; %> We will need to do this to all views that handle errors (obviously changing the status code to the one appropriate for that particular error). This not only includes the pages you’ve configured in the customErrors element in the Web.config, but also any views you are using with ASP.NET MVC HandleError attributes (if you’re using ASP.NET MVC). After making these changes, our Fiddler trace looks like this: A tra