Asp.Net MVC catch 404 for unknown extensions - asp.net-mvc

I'm able to catch almost every 404 but this:
http://example.com/mariajosefa.pedro de heredia
The file "mariajosefa.pedro de heredia" doesn't exists.
This seems to be catched by IIS and my users are presented with the standard IIS 404 error page.
Is this possible?

Sorry for the quick answer :( for reference, this is what works for me:
In web.config:
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" path="/Home/NotFound" responseMode="ExecuteURL"/>
</httpErrors>
ALSO REMOVE THE COLON FROM THE INVALID CHARACTERS LIST, THIS WAS ALSO BOTHERING ME
<httpRuntime targetFramework="4.5.1" requestPathInvalidCharacters="<,>,*,%,&,\,?"/>

Related

Elmah not logging 404 errors after third folder, and 403 errors at all

Continuing my previous question, I handle custom errors in my mvc5 app using:
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404"/>
<error statusCode="404" path="/Errors/NotFound" responseMode="ExecuteURL" />
<remove statusCode="403"/>
<error statusCode="403" path="/Errors/NotFound" responseMode="ExecuteURL" />
<remove statusCode="500"/>
<error statusCode="500" path="/Errors/ServerError" responseMode="ExecuteURL" />
</httpErrors>
...
</system.webServer>
This enabled me to show custom 404 error page after the 3rd folder (e.g. site.com/a/b/c/nothing-here). Yet, Elmah is not logging these errors. This is also true for 403 errors. So My questions are:
is there a way to make Elmah handle all server errors regardless of
being thrown by the mvc app itself?
if not, since /Errors/NotFound is successfully executed, I can add a log from that action. is there a way to tell if Elmah already handled this error (or was it thrown by the mvc app)?

How to set Error page in webconfig for mvc web based system

I have MVC 4 web based system. I need to handle error page in web.config file. I m try do with httpError syntax. When 404 it will show blank page. not show the actual error page. url also not show as error page.
web.config code:
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" />
<error statusCode="404" responseMode="ExecuteURL" path="~/Error/PageNotFound.htm" />
</httpErrors>
please help this.
Try looking at this page: Error Handling and Nice Error pages.
It says to add the following in the Web.Config:
<customErrors mode="On" defaultRedirect="~/Content/Errors/page500.aspx" redirectMode="ResponseRewrite">
<error statusCode="404" redirect="~/Content/Errors/page404.aspx" />
</customErrors>
I haven't got my MVC code infront of me but I think this is what I did.

Is it possible to use custom error pages with MVC site but not Web API?

I have an MVC project with a /api folder which contains my Web API controllers. I want the following things:
My MVC site to serve a custom error page when an error occurs
My Web API to serve the default error response (json/xml containing exception and stack trace)
In the web.config for my MVC site, I have an httpErrors node, and have set the errorMode to "Custom" so that I can have nice error pages when 404s/500s/etc. occur during browsing of the MVC site:
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" subStatusCode="-1" />
<remove statusCode="500" subStatusCode="-1" />
<remove statusCode="403" subStatusCode="-1" />
<error prefixLanguageFilePath="" statusCode="404" path="Content\notfound.htm" responseMode="File" />
<error statusCode="500" path="/Errors" responseMode="ExecuteURL" />
<error statusCode="403" path="/Errors/Http403" responseMode="ExecuteURL" /></httpErrors>
With that configuration however, the API will serve the custom error page when an error occurs, not json/xml with the exception/stack trace (which is the desired behavior).
Is there a way to configure custom errors to only apply to my MVC site and not the Web API? This blog says there is not (http://blog.kristandyson.com/2012/11/iis-httperrors-aspnet-web-api-fully.html), but I'd like to hear if anyone else has found a work around since that blog was published.
I suppose if there is not I could create a separate project/assembly for my Web API project. That would allow me to configure httpErrors for MVC and Web API separately, but I would prefer not to create another project just so I have yet another web.config to configure.
Well, after a nearly a year of letting this question marinade, I gave it another shot. Here's the web.config magic that got me what I wanted:
<!-- inside of <configuration> to allow error
responses from requests to /api through -->
<location path="api">
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<httpErrors errorMode="DetailedLocalOnly" existingResponse="PassThrough" >
<clear/>
</httpErrors>
</system.webServer>
</location>
<!-- original httpErrors, inside of <location path="." inheritInChildApplications="false">
to serve custom error pages when the MVC site returns an error code -->
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="400" subStatusCode="-1" />
<remove statusCode="404" subStatusCode="-1" />
<remove statusCode="500" subStatusCode="-1" />
<remove statusCode="403" subStatusCode="-1" />
<error statusCode="400" prefixLanguageFilePath="" path="Content\notfound.htm" responseMode="File"/>
<error prefixLanguageFilePath="" statusCode="404" path="Content\notfound.htm" responseMode="File" />
<error statusCode="500" path="/errors" responseMode="ExecuteURL" />
<error statusCode="403" path="/errors/http403" responseMode="ExecuteURL" />
</httpErrors>
The crux of what's going on here is that the <location> node allows you to override settings made at a less specific path. So while we have errorMode="Custom" for path=".", we override that for the our Web API's path with the <location path="api"> node, and the httpErrors configuration within it.
I had seen nodes before, but it didn't dawn on me that this was their purpose until now. This article goes into more detail on the configuration inheritance model of IIS/.NET, which I found very informative: http://weblogs.asp.net/jongalloway/10-things-asp-net-developers-should-know-about-web-config-inheritance-and-overrides
I have not tested this, but how about writing the code to handle your MVC application errors, like shown here http://thirteendaysaweek.com/2012/09/25/custom-error-page-with-asp-net-mvc-4/ ?
His code show that he is doing this at the application level (Global.asax), but I guess you could just as well trap exceptions at a lower level in the same way, with one method for MVC and another one for Web API.
What have worked for me:
<httpErrors existingResponse="Auto" defaultResponseMode="Redirect" errorMode="Custom">
<remove statusCode="403" subStatusCode="-1" />
<error statusCode="403" subStatusCode="-1" responseMode="Redirect" path="<!--path here-->" />
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" subStatusCode="-1" responseMode="Redirect" path="<!--path here-->" />
<remove statusCode="500" subStatusCode="-1" />
<error statusCode="500" subStatusCode="-1" responseMode="Redirect" path="<!--path here-->" />
</httpErrors>
Seems like just setting existingResponse="Auto" will do the job.

Custom error pages with HandleError attribute

I have asp.net web application and try to specify custom HTML for Dynamic Ip Restrictions module, which returns 401 prior to .net code.
I've added following to my web.config:
<httpErrors>
<remove statusCode="404" subStatusCode="-1" />
<remove statusCode="401" subStatusCode="-1" />
<error statusCode="401" path="ddos.html" responseMode="File" />
<error statusCode="401" subStatusCode="502" path="ddos.html" responseMode="File" />
<error statusCode="404" prefixLanguageFilePath="" path="/404" responseMode="ExecuteURL" />
</httpErrors>
Productin environment (windows Server 2008): Pages with 404 and 401 works fine, but exception handling is broken - i see built-in iis "red" page instead of custom page.
Development environment (windows 7/8): 404 and erors are fine, but 401 redirect user to login page.
Exception handling defined as:
[HandleError(View = "Error", Master = "~/Views/Shared/_ThreeColumnsLayout.cshtml")]
UPD1: Also, I can add that if I remove pages with code 500, I see foloowing plain text message:
The page cannot be displayed because an internal server error has occurred.
<httpErrors existingResponse="Auto">
does the trick

MVC 2 customErrors works locally but not live?

Here is what I have in my web.config:
<customErrors mode="On" defaultRedirect="/pages/sitemap">
<error statusCode="404" redirect="/pages/sitemap" />
</customErrors>
Locally this works just as expected but live for some reason I still get the vanilla 404 error page?
I am using MVC 2 with Areas, thoughts?
Just a quick thought, have you tried adding the ~ to your redirects? Your application/directory structure is probably different on PROD than on your local test machine.
<customErrors mode="On" defaultRedirect="~/pages/sitemap">
<error statusCode="404" redirect="~/pages/sitemap" />
</customErrors>
Also, take a look if you have the something similar to the following snippet in your web.config
<httpErrors errorMode="DetailedLocalOnly">
<error statusCode="403" subStatusCode="4" path="custerr\403.4.htm" responseMode="File" />
</httpErrors>
This 'could' be overriding your settings.

Resources