Soulfire
Soulfire

Reputation: 4296

How can I turn customErrors mode on ASP.NET MVC4?

I am very new to ASP.NET MVC4 and am taking a course on the topic. I have a very minor issue with error handling. When the instructor adds <customErrors mode = "On"/> to the web.config file in the <system.web> tag, he is redirected to the friendly error page (instead of the stack trace).

When I make this change, I am still directed to the stack trace "yellow page of death".

Since I am making a concerted effort to understand this as thoroughly as possible, I thought I'd ask here. Why does turning <customErrors mode="On"/> work for the instructor and not me?

Here is my <system.web> tag:

<system.web>

    <customErrors mode="On"/>

    <httpHandlers>
      <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
    </httpHandlers>
    <pages
        validateRequest="false"
        pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <controls>
        <add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
      </controls>
    </pages>

</system.web>

And here is my Global.asax.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace OdeToFood
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();
        }
    }
}

And finally, my FilterConfig.cs

using System.Web;
using System.Web.Mvc;

namespace OdeToFood
{
    public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }
    }
}

I currently have the default view Error.cshtml.

I also tried

<customErrors mode="On" defaultRedirect="Error"/>

and

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

I am using this code to force the error:

    public class CuisineController : Controller
    {
        //
        // GET: /Cuisine/
        public ActionResult Search(string name = "french")
        {

            throw new Exception("Something terrible has happened");

            var message = Server.HtmlEncode(name);

            return Content(message);
        }
    }
}

As you can tell, I'm very new to this so my apologies if this is a frivolous question but I am trying to learn as best I can.

Upvotes: 3

Views: 419

Answers (1)

Win
Win

Reputation: 62260

You still need to make an error page. For example,

Web.config

<system.web>
  ...
  <customErrors defaultRedirect="~/Common/Error" mode="On"/>
</system.web>

CommonController

public class CommonController : Controller
{
    // Error
    public ActionResult Error()
    {
        this.Response.StatusCode = 503;
        this.Response.TrySkipIisCustomErrors = true;

        return View();
    }
}

~/Views/Shared/Error.cshtml

@{
    ViewBag.Title = "Error";
}
<h2>@ViewBag.Title</h2>
<p>
    Error ...
</p>

Upvotes: 4

Related Questions