Reputation: 5370
I have the following in my web.config. This works fine but i'm wondering is it possible to catch the exception on this page. So lets say i throw an exception on "/Products/12345" i would like to write the exception message on "/Error"
<customErrors mode="On">
<error statusCode="404" redirect="/Error" />
</customErrors>
Upvotes: 0
Views: 262
Reputation: 30152
Yes - when you setup a new "Internet mvc app" template it is already setup for you at /Shared/Error.cshtml but you have to change it a bit to handle the details. MVC3 uses [HandleError] globally to handle this.
Change the default page to be HandleErrorInfo such as:
@model System.Web.Mvc.HandleErrorInfo
@{
ViewBag.Title = "Error";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Error</h2>
<p>
Controller: @(((HandleErrorInfo)ViewData.Model).ControllerName)
</p>
<p>
Action: @(((HandleErrorInfo)ViewData.Model).ActionName)
</p>
<p>
Message: @(((HandleErrorInfo)ViewData.Model).Exception.Message)
</p>
<p>
Stack Trace: @(((HandleErrorInfo)ViewData.Model).Exception.StackTrace)
</p>
Upvotes: 1