7

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 ?

4 Answers 4

3

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"> 
Sign up to request clarification or add additional context in comments.

2 Comments

CustomErrors mode was already ON. Else if it would have been off then even the default error page would not show up. Any other ideas ???
Actually i have no idea about the MVC.
2

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> 

1 Comment

Didn't worked. Even If i remove the default redirect attribute, it takes me to my defaultview of error. so definitely i guess it has got no role to play here.
1

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.

Comments

0

make sure your "notfound" action and "detail" action in the same controller. Otherwise, you should specifiy the controller name, or put the notfound.aspx under the shared folder

2 Comments

It is under shared folder only. And notfound is Not an Action, just a view.
Sorry, I misunderstood it before! Did you throw an Http 404 from the action method ? Actually, if the expcetion is not an http 500,the HandleError will ignore it. Maybe this is the reason why always show the default view.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.