I have implemented exception handling as mentioned in below link
How to pass error message to error view in MVC 5?
It is working fine. But I have requirement to handle 404 Error
.
How can I do that?
if I use below code,
<customErrors mode="On">
<error statusCode="404" redirect="/Home/Error"></error>
</customErrors>
it works well when any 404
error occurs. But in case any other exception occurs then my error.cshtml
call twice and show same exception two times
.
throw new HttpException(404, "File Not Found"); return View(); you will invoke the error handelers but it will not set the Status code. throw new HttpException(404, "File Not Found"); return HttpNotFound();
To resolve this problem, use one of the following methods: Make sure that the requested resource is at the location that the URL points to. Review the URL that you open in the Web browser. Make sure that no custom filters or modules restrict access to the file that you want to browse.
But if user passes article ID that doesn't exists the page must return code 404 (Not Found) and show the custom 404 page like in the previous situation.
If you will define defaultRedirect attribute for customErrors then error.cshtml will be rendered only once in your case:
<customErrors mode="On" defaultRedirect="/Home/Error">
<error statusCode="404" redirect="/Home/Error"/>
</customErrors>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With