I have a MVC ASP.NET website in which I have the following on the web.config
<customErrors mode="On" defaultRedirect="~/Views/Shared/Error.cshtml">
<error statusCode="404" redirect="~/redir/Home/Http404" />
</customErrors>
So when I try to navigate to an address that does not exist I get my Http404 view located under Home which is correct.
Now I have a special situation in which I want to manually return the same Http 404 and I am seeing the IIS 8 error instead of my Http404 view.
Controller :
return new HttpNotFoundResult();
How to avoid this IIS8 error page, and still make my view the error provider?
EDIT*
I found out that if instead of HttpNotFoundResult() I use the old throw new HttpException(404, "HTTP/1.1 404 Not Found"); it works.
Why this difference ?
Thanks
The <customErrors> section is aimed to control the behavior when an Application Error occurs. That's also why it's defined under the <system.web> section.
When you explicitly throw an HttpException (or when any other internal exception occurs) the <customErrors> settings will be used as it's considered (rightfully) an application error.
On the other hand, when you simply return HttpNotFoundResult(), no application error is being thrown (internally, it just sets the HTTP response status code and message to 404) and the MvcHandler will hand over the request to the next handler in the pipeline. So you eventually end up with a regular Server Error that is handled by IIS just like any other '404 not found' request, and the <customErrors> section does not affect its behavior.
So, you need to control IIS behavior when regular server error occurs. To do this, use the <system.webServer/httpErrors> section as follows:
<system.webServer>
<httpErrors errorMode="Custom">
<remove statusCode="404"/>
<error statusCode="404" path="/redir/Home/Http404" responseMode="ExecuteURL" />
</httpErrors>
</system.webServer>
See Documentation
Related
I am developping an Asp.NET web application with visual studio 2015 and running it under IIS Express. I want to add a custom error page to it.
First, in the web.config file, I added customErrors markup under the system.web section. But it didn't seem to have any effect.
<customErrors mode="On" defaultRedirect="/Error.aspx">
<error statusCode="404" redirect="/Error.aspx" />
</customErrors>
So I added an httpErrors markup under system.webServer. And it showed me a blank page.
<httpErrors existingResponse="PassThrough" />
So I changed the above httpErrors markup by this one :
<httpErrors errorMode="Custom" existingResponse="Auto" defaultResponseMode="ExecuteURL" defaultPath="/Error.aspx">
<clear/>
<error path="/Error.aspx" responseMode="ExecuteURL" statusCode="404"/>
</httpErrors>
And it showed me the "lock violation" error. I Tried to remove ,defaultPath in the applicationHost.config file, rebooted the computer, but the same error appears
Even in Global.asax, the Application_Error is not fired in the case of some error like 404.
Does someone have any clue ?
Edit :
I tried to do the same thing in a new solution :
So I created a new web application - Blank webforms template. Same result.
But when I created a new web application - Blank MVC solution, it worked.
Is there any difference in the config between webforms and mvc? Is someone else have the same issue whith the webforms template ?
I was able to redirect to error page. I tried this in my current MVC project. Below is excerpt of what i did
Used existing error page in my solution under Views/Shared/Error.cshtml
Used your custom errors parameters
<customErrors mode="On" defaultRedirect="~/Views/Shared/Error.cshtml">
<error statusCode="404" redirect="~/Views/Shared/Error.cshtml" />
</customErrors>
Threw 404 purposefully in my Index.cshtml
#Response.StatusCode=404
#Response.End()
Hint: Please try to see the path and how you are testing 404 error
I'm trying to redirect all my 404 errors to the Index page of my project.
But when I set my custom errors in web.config I get a Runtime Error that says:
"An exception occurred while processing your request. Additionally, another exception occurred while executing the custom error page for the first exception. The request has been terminated. "
Here is my web.config errors block.
<customErrors mode="On" redirectMode="ResponseRewrite" >
<error statusCode="404" redirect="~/" />
</customErrors>
I would appreciate any suggestions how to debug and fix it.
Your redirect is empty. Try adding a new custom error page and change the redirect to something like redirect="~/Controller/CustomerErrorPage"
I have an MVC application that is running on IIS. I have created a custom exception filter handler that returns a custom JSON object. When I run it locally everything works correctly.
However, when I deploy it to IIS the correct HTTP Status Code is returned, but instead of getting my custom JSON object, I am getting a plain text response that says "Bad Request" or "The resource you are looking for has been removed, had its name changed, or is temporarily unavailable."
In my web config, I have added:
<customErrors mode="Off" />
and
<httpErrors>
<clear />
</httpErrors>
Any suggestions would be greatly appreciated.
Thanks!
I found the solution. I needed to add errorMode = "Detailed to my httpErrors tag.
<httpErrors errorMode="Detailed">
<clear />
</httpErrors>
Ive got an interesting issue happening.The site is getting a bunch of the error messages from other sites (not my own):
Exception type: HttpRequestValidationException
Exception message: A potentially dangerous Request.Form value was detected from the client (comment="...er[/url] <a href="http://obey...").
Request path: /404_custom.aspx
The source host is not my server IP and the URL is from a completely different URL however it is ending up in my Event Viewer. Im assuming this is some kind of spambot that is causing issues - or potentially i have stumbled upon a problem with my IIS application.
We have the following in our web.config:
<httpErrors>
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="/404_custom.aspx" responseMode="ExecuteURL" />
</httpErrors>
I understand that these errors are caused by corss-site scripting, but why would the host of the site be another site thats not my own? Could it do with the bindings set against the IIS application?
Does anyone know of why this would be happening? Is this caused by a spambot or some sort of attack? Anyone have any words of widsom?
in my C# code I use try/catch on all my methods, this picks up most of the errors, but some errors like 404 are not picked up.
Does anyone know how to pick up all other errors that occur on the server or if SQL server is down?
thanks
you should use ELMAH as error handler in your project...read the link for more details.
You can update your web.config file and specify the following:
<customErrors mode="On">
<error statusCode="404" redirect="~/error404.aspx" />
</customErrors>