I need to implement my own custom error page in MVC 4. Basically when users try to view Details
of any product with non-existent productID, I want this custom error page.
I created my own custom error page NotFound.aspx
The inside content of this page is :
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Simple.Master"
Inherits="System.Web.Mvc.ViewPage<System.Web.Mvc.HandleErrorInfo>" %>
<asp:Content ID="errorTitle" ContentPlaceHolderID="TitleContent" runat="server">
Error
</asp:Content>
<asp:Content ID="errorContent" ContentPlaceHolderID="MainContent" runat="server">
<h2>
Sorry, you requested a product that doesn't exist. Make sure you
requested a valid ProductID
</h2>
</asp:Content>
And then Applied the HandleError
filter to my ActionMethod: Details
as:
[HandleError(View="NotFound")]
public ActionResult Details(int id) {...
The problem is that, Always the default view : Views/Shared/Error.aspx
is being called and not the new custom error page. Any thoughts ?
Try this (but i am not sure about working of this code in MVC). Paste this code in configuration section in web.config file brlow system.web.
<customErrors mode="On" defaultRedirect="ErrorPage.aspx">
</customErrors>
<compilation debug="true" targetFramework="4.0">
Try using custom errors tag found in the web config file that might help you.
Here is the sample
<system.web>
<--- other required may be used here--->
<customErrors mode="On" defaultRedirect="ErrorPage.aspx"></customErrors>
</system.web>
The solution is that my ProductController
class also needs to have the order
property set as:
[HandleError(Order=2)]
public class ProductController : Controller { ... }
What this means: An Order value of 2 will ensure that the controller-wide filter will be applied only if there isn’t a HandleError filter with a higher order available.
And this worked perfectly. My web.config setting are:
<customErrors mode="On" />
.
This is it. The defaultRedirect
is not needed at all.
NOTE:
I had the HandleError
filter initially with NO order
property.
[HandleError]
public class ProductController : Controller { ... }
When you apply the HandleError filter with no arguments, you are specifying that any exception
thrown by the methods covered by the filter will result in the Views/Shared/Error.aspx
view being used.
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