Best way to implement a 404 in ASP.NET

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 (serverError != null){ if (serverError.GetHttpCode() == 404){ Server.ClearError(); Server.Transfer(“/Errors/404.aspx”); } } } In you error page, you should ensure that you’re setting the status code correctly: … Read more

How can I catch a 404?

try { var request = WebRequest.Create(uri); using (var response = request.GetResponse()) { using (var responseStream = response.GetResponseStream()) { // Process the stream } } } catch (WebException ex) { if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null) { var resp = (HttpWebResponse) ex.Response; if (resp.StatusCode == HttpStatusCode.NotFound) { // Do something } else { // … Read more

CakePHP 2.0 – How to make custom error pages?

Try this: /app/Config/core.php Exception render need to set as an AppExceptionRender. Example: Configure::write(‘Exception’, array( ‘handler’ => ‘ErrorHandler::handleException’, ‘renderer’ => ‘AppExceptionRenderer’, ‘log’ => true )); /app/Controller/ErrorsController.php class ErrorsController extends AppController { public $name=”Errors”; public function beforeFilter() { parent::beforeFilter(); $this->Auth->allow(‘error404’); } public function error404() { //$this->layout=”default”; } } /app/Lib/Error/AppExceptionRenderer.php App::uses(‘ExceptionRenderer’, ‘Error’); class AppExceptionRenderer extends ExceptionRenderer { public … Read more

HTTP 404 Page Not Found in Web Api hosted in IIS 7.5

I was struggling with this as well. Fortunately, Steve Michelotti documented a solution that worked for me here. At the end of the day, I enabled all verbs (verb=”*”) to the ExtensionlessUrlHandler-Integrated-4.0 handler in my web config. <system.webServer> <validation validateIntegratedModeConfiguration=”false” /> <modules runAllManagedModulesForAllRequests=”true” /> <handlers> <remove name=”ExtensionlessUrlHandler-Integrated-4.0″ /> <add name=”ExtensionlessUrlHandler-Integrated-4.0″ path=”*.” verb=”*” type=”System.Web.Handlers.TransferRequestHandler” resourceType=”Unspecified” requireAccess=”Script” … Read more

HTTP Status 404 – The requested resource (/) is not available

What are you expecting? The default Tomcat homepage? If so, you’ll need to configure Eclipse to take control over from Tomcat. Doubleclick the Tomcat server entry in the Servers tab, you’ll get the server configuration. At the left column, under Server Locations, select Use Tomcat installation (note, when it is grayed out, read the section … Read more

How to make a catch all route to handle ‘404 page not found’ queries for ASP.NET MVC?

Found the answer myself. Richard Dingwall has an excellent post going through various strategies. I particularly like the FilterAttribute solution. I’m not a fan of throwing exceptions around willy nilly, so i’ll see if i can improve on that 🙂 For the global.asax, just add this code as your last route to register: routes.MapRoute( “404-PageNotFound”, … Read more

HTTP Status 404 – The requested resource (/ProjectName/) is not available

By default, when you open the project root folder as in http://localhost:8080/ProjectName/ instead of a physical file as in http://localhost:8080/ProjectName/Index.jsp, then the server will lookup for a welcome file in web.xml. If none is found, then you’ll get this 404 “Page not found” error. In your case, URLs and file names are case sensitive. You … Read more