Asp.net Catch 404 Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies asp.net error handling of this site About Us Learn more about Stack Overflow the company exception handling in asp.net c# Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges asp.net custom error Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: asp.net error page Sign up Best way to implement a 404 in ASP.NET up vote 28 down vote favorite 12 I'm trying to determine the best way to implement a 404 page in a standard ASP.NET web application. I currently catch 404 errors in the Application_Error event in the Global.asax file and redirect to a friendly 404.aspx page. The problem is that the request sees
Asp.net Error Logging
a 302 redirect followed by a 404 page missing. Is there a way to bypass the redirect and respond with an immediate 404 containing the friendly error message? Does a web crawler such as Googlebot care if the request for a non existing page returns a 302 followed by a 404? asp.net http-status-code-404 share|improve this question asked Mar 20 '09 at 17:01 Ben Mills 7,782112936 add a comment| 9 Answers 9 active oldest votes up vote 29 down vote accepted Handle this in your Global.asax's OnError event: protected void Application_Error(object sender, EventArgs e){ // An error has occured on a .Net page. var serverError = Server.GetLastError() as HttpException; if (null != serverError){ int errorCode = serverError.GetHttpCode(); if (404 == errorCode){ Server.ClearError(); Server.Transfer("/Errors/404.aspx"); } } } In you error page, you should ensure that you're setting the status code correctly: // If you're running under IIS 7 in Integrated mode set use this line to override // IIS errors: Response.TrySkipIisCustomErrors = true; // Set status code and message; you could also use the HttpStatusCode enum: // System.Net.HttpStatusCode.NotFound Response.StatusCode = 404; Response.StatusDescription = "Page not f
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
Asp.net Mvc Error Handling
Dev centers Retired content Samples We’re sorry. The content you requested has been asp.net application_error removed. You’ll be auto redirected in 1 second. MSDN Library MSDN Library MSDN Library MSDN Library Design Tools asp.net error handling best practices Development Tools and Languages Mobile and Embedded Development .NET Development Office development Online Services Open Specifications patterns & practices Servers and Enterprise Development Speech Technologies Web Development Windows Desktop App Development http://stackoverflow.com/questions/667053/best-way-to-implement-a-404-in-asp-net TOC Collapse 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. Complete Example for Error Handlers Other Versions Visual Studio 2010 .NET Framework 4 Visual Studio 2008 This code example includes elements for both page-level and application-level exception handling. Code Example Files The example consists https://msdn.microsoft.com/en-us/library/bb397417.aspx of the following files: Web.config Global.asax Default.aspx ExceptionUtility (to be put in the App_Code folder) GenericErrorPage.aspx HttpErrorPage.aspx Http404ErrorPage.aspx DefaultRedirectErrorPage.aspx Web.config The following example shows the Web.config file. The customErrors section specifies how to handle errors that occur with file types that are mapped to ASP.NET, such as .aspx, .asmx, and .ashx files. (In IIS 6.0 and in IIS 7.0 in classic mode, static content files such as .html and .jpg files are not mapped to ASP.NET.) The settings in the example customErrors section cause any unhandled HTTP 404 (file not found) errors to be directed to the Http404ErrorPage.aspx file. These HTTP 404 errors would occur if a request were made for an .aspx file, .asmx file, and so on and if the requested file did not exist. All other unhandled errors in ASP.NET files are directed to the DefaultRedirectErrorPage.aspx file. If static content files are not handled by ASP.NET, a request for a nonexistent .html or .jpg file does not cause a redirect to the Http404ErrorPage.aspx file. If you want ASP.NET to handle requests for all file types, you can configure IIS to map fi
October 02, 2009 3:21 PM As mentioned at the end of my previous post on handling errors with ASP.NET, handling "404 Not Found" errors are particularly problematic http://www.andornot.com/blog/post/Handling-404-errors-with-ASPNET.aspx (if you haven't read it yet, please do so). And looking around, the vast majority of information out there on it is not complete, misinformed, or flat-out wrong (but I greatly appreciate all https://www.stokia.com/support/misc/web-config-custom-httperrors.aspx efforts!). And I would argue that this is because ASP.NET implementation of 404 error handling is flat-out-wrong. So with my super hero cape on, here I come to wobbly save the day! The asp.net error typical ASP.NET way to handle 404 errors is to put something like the following in your Web.config:
your web site. The custom errors can be set or overridden on a site wide or directory-by-directory basis. While some web.config sections require that the directory is set as an application, this isn't one of them. A simple web.config with a httpErrors section may be placed in any directory, and the directory does NOT need to be set as an application. What are http errors? HTTP errors are returned to the client when something goes wrong on the server. Error status codes are returned if the requested file isn't found (404), or due to coding errors in the web page (500), and due to temporary issues such as failed database connections (500). The most common errors are 404 (file not found) and 500 (application) errors. Custom 404 and 500 errors are typically used to provide a friendlier error message to your users. Custom 404 and 500 errors could also redirect the user to the default (or any) page, and are sometimes used to notify the web site administrator of problems on the web site. If you wish to configure custom errors for your site, or even just for a single directory in your site, please follow the directions on this page. 400 Error (bad request) 401 Error (unauthorized) 403 Error (forbidden) 404 Error (not found) 500 Error (internal server error) How it's done Example custom HTTP errors. Comments are enclosed in and are not required. Capture and return specific error types