views:

46

answers:

3

Hello,

I have created a new MVC Application in VS2010 straight out the box. In the Home Controller I have ther following:

    [HandleError(View = "Error")]
    public ActionResult Index()
    {


        int num1 = 0;
        int num2 = 5;

        int result = num2 / num1;

        return View();

    }

This creates an error and a debug pop up and then if I continue a yellow screen. I cannot get it to redirect to the Error.aspx page. I have the web.config like this:

 <customErrors mode="On" defaultRedirect="~/Error.aspx">
 </customErrors>

but it will not redirect to the correct error page despite being a brand new project. How do I configure this to work please????

thanks

+1  A: 

This is discussed in the MVC overview tutorials. The behaviour of IIS errors is dependent on your version of IIS.

Using ASP.NET MVC with Different Versions of IIS (C#)

Philip Smith
I cannot see anything about error handling from that link - a good resource though. I have IIS 7.0
Oops! Wrong answer. You might try this http://stackoverflow.com/questions/619895/how-can-i-properly-handle-404s-in-asp-net-mvc/2577095#2577095
Philip Smith
Thanks great link. I just create a new mvc project and threw an error in the Index action of the home controller and it worked. I don't understand why it didnt work the first time and I never will. I still get the debugger kicking in though. What mode do I need to be in to stop this from happening? and how do I put it in that mode??
A: 

Try this blog post: http://msnetprogrammer.net/blog/post/HandleError-attribute-%28MVC%29.aspx

Also the default View name is Error so you technically can just have [HandleError]

amurra
A: 

If I try to replicate this I get redirected to the default error view within the Shared view folder - it looks like MVC overrides any setting made to the defaultRedirect attribute in the web.config.

In your web.config you are trying to redirect to a physical aspx page. The HandleError attribute on your action will attempt to find a view named Error first within your specific view folder, moving on to the shared view folder. Just pick one or the other and you should be fine.

James Antrobus