In asp.net mvc is it possible to redirect to a custom error page when a static file is is not found rather than have IIS serve an error page?
The example below, adding this to System.Web in the web.config file will redirect the user to an Error controller and return an Error page view.
<customErrors mode="RemoteOnly" defaultRedirect="~/Error" redirectMode="ResponseRedirect">
<error statusCode="404" redirect="~/Error" />
</customErrors>
That is fine if the url resembels a controller and action but if a static file was typed into the url such as mysite.com/myFile.html, the custom error page isn't returned and IIS serves an Error page. Is it possible to have this handled within the mvc application, perhaps by redirecting the user to the same Error controller that the above custom error handler redirects to?
Related
I have a simple objective; I just want -every- single 404 error to redirect to a Single-Page Application for the SPA to handle routing.
This seems to work for routes that could possibly exist within the ASP.NET app (based on existing Controllers). However, if I navigate to a route that couldn't not possibly have been an ASP.NET route (which is happening, as some routes only exist client-side in the Vue app), it seems like IIS is immediately taking charge of the request and issuing this simple text/html response:
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
The response is issued almost immediately from receipt of the request, which seems to indicate it is being immediately handled as a 404 by IIS before any .NET managed handlers touch it.
How do I stop IIS from doing this?
Additional Info:
ASP.NET Web.config error fallback is already in place:
<system.webServer>
<httpErrors>
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="/vue-fallback.html" responseMode="File" />
</httpErrors>
</system.webServer>
This is an ASP.NET MVC Server; System.Web.Mvc v4.0.0.0.
IIS Version: IIS 8
ETA:
I ran the server locally (not sure why I didn't do this sooner), the exact error seems to be a failure in the StaticFile handler.
HTTP Error 404.0 - Not Found
...
Module IIS Web Core
Notification MapRequestHandler
Handler StaticFile
Error Code 0x80070002
This seems to solve my issue:
routes.MapRoute(
"Vue-Fallback",
"{*catchall}",
new { controller = "Home", action = "VueFallback" }
);
I am working to ASP.net MVC-5 project and I am a beginner. As of now I know the basics of MVC and able to display pages and make post and get request. But I am stuck with one issue where I get "504 gateway time out error" and I need to implement one functionality where if the client gets `504 gateway timeout error" I should be able to show a particular page like "Something is wrong" instead of standard error. To start with below is what I did.
<system.web>
<compilation debug="true" targetFramework="4.5.1" />
<httpRuntime targetFramework="4.5" />
<customErrors mode="On" redirectMode="ResponseRewrite">
</customErrors>
</system.web>
But what else I need to change or add. Can someone guide me here. I am lost.
If your ASP.NET MVC application is hosted by the IIS (or another proxy server), then the 504 error is returned by the IIS and sent to the browser, because your application does not respond to the request in a timely manner. The IIS has got a timeout (configurable for each web site) and if this is reached, then 504 is returned.
In this scenario you cannot define a custom error page inside your application, because it is not reacting.
You can configure the IIS to server custom error pages. This video tutorial shows how to do this.
ASP doesn't actually return those pages, that's actually IIS.
Check out this SO on configuring IIS to let you return custom error pages
Basically you'll need to configure these settings:
<system.web>
<customErrors mode="On" defaultRedirect="~/Error">
<!--Use whatever paths you need here, along with the accompanying status code -->
<error redirect="~/Error/NotFound" statusCode="404" />
</customErrors>
</system.web>
And create a controller to handle the errors, like so:
public class ErrorController : Controller
{
public ViewResult Index()
{
return View();
}
public ViewResult NotFound()
{
Response.StatusCode = 404;
return View();
}
}
Along with whatever view you need to match the Action.
With multi-language sites in umbraco, 404 pages can be set in umbracoSettings.config so that the content can be translated.
<errors>
<error404>
<errorPage culture="default">//yourDocumentType[#nodeName='404']</errorPage>
</error404>
Normally setting other error codes would be set in the Web.config that would redirect to a static .net page.
<customErrors mode=“On“ defaultRedirect=“Error.aspx“>
<error statusCode=“403“ redirect=“Error.aspx“ />
<error statusCode=“404“ redirect=“Error.aspx“ />
</customErrors>
Is there a way of being able to set HTTP status codes for other languages like you can with 404 error codes in umbracoSettings.config
Sadly, as far as I know, this only works for 404 pages, I don't think you can use it to translate error pages for other status codes, sorry!
I feel like I may be over complicating things with a guide like this one:
How to override all standard error pages in WebAPI
I couldn't get it to work because I think the version of ASP.NET Web API that I'm using has deprecated the context.Request.CreateErrorInfoResponse()
All I want to do is display an HTTP 403 Forbidden as a single static file located in ~/Error.json
I've tried doing this to my Web.config file:
<system.web>
<customErrors mode="RemoteOnly">
<error statusCode="403" redirect="~/Error.json" />
</customErrors>
</system.web>
And I've tried this too:
<system.web>
<customErrors mode="On">
<error statusCode="403" redirect="~/Error.json" />
</customErrors>
</system.web>
It doesn't seem to have any effect at all when this exception is thrown:
throw new APIException(HttpStatusCode.Forbidden, new MessageError("foobar"));
In my application, there will be only one situation where a Forbidden response is received, and I don't want my RESTful API to be returning an HTML response in this event.
What can I do to fix this?
The reason why I need to do this is to render the Web Application useless if a customer tries to crack or pirate this web application. I would like to do all of this within Startup.cs if it's possible to crash the app and to return a json response of the error rather than an HTML response that the throw new Exception method does by default.
What web.config settings should I enter so that I can handle 404 errors but not other errors. The reason I want to handle 404 errors is that sometimes I purposely enter urls for inexisting pages but which I can handle and internally redirect to the real pages. That works fine, however, I do not want to handle other pages. On the contrary, I want to load the standard "non-custom" errors so that I can identify what is wrong with the page and see the line number causing the error. But when I enable custom errors for 404, I start getting redirected to the default error page (for 500 errors) and not have a clue of what caused the error.
Alternately I would consider having a custom error page that properly displays which page and which line is causing the error. I could do that for classic ASP but I could not find how to do that for ASP.Net.
Any help or pointers would be greatly appreciated.
Thanks!
P.S. not sure if it makes a difference, but my test server is IIS7.5 while the one for the live website is IIS6.
Try this:
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" path="/Errors/404.aspx" responseMode="ExecuteURL"/>
</httpErrors>
Please refer to the following post:
http://tedgustaf.com/en/blog/2011/5/custom-404-and-error-pages-for-asp-net-and-static-files/