MVC 4 project has already has an Error.cshtml in Views\Shared folder. In order to use the Error.cshtml, you have to do following steps:
1. Set the customError mode to On inside web.config file under <system.web> element
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
}
3. Edit Error.cshtml inside \Views\Shared folder to show error message
@model System.Web.Mvc.HandleErrorInfo
@{
ViewBag.Title = "Error";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div>An error has occurred</div>
@if (Model != null && HttpContext.Current.IsDebuggingEnabled)
{
<div>
<p>
<b>Exception:</b> @Model.Exception.Message<br />
<b>Controller:</b> @Model.ControllerName<br />
<b>Action:</b> @Model.ActionName
</p>
<div style="min-height: 400px;overflow:auto">
<pre>
@Model.Exception.StackTrace
</pre>
</div>
</div>
}
else
{
<p>@Model.Exception.Message</p>
}
4. The system will show the Error page in the following conditions:
1) When there is uncaught Exceptions, the system will automatically call this Error view and pass the Exception information
2) You can manually redirect to this view in your error handling code:
protected ActionResult GotoErrorPage(string strErrorMessage)
{
string controller = this.ControllerContext.RouteData.Values["controller"].ToString();
string action = this.ControllerContext.RouteData.Values["action"].ToString();
return View("Error", new HandleErrorInfo(new System.Exception(strErrorMessage)), controller, action));
}
5. If you want to create your own customized error page for HTTP status code 401, 404 etc, you need add more <error redirect /> configuration inside <customErrors> element inside web.config file. However, you need to create your own ErrorController and error views to handle different situation.
1. Set the customError mode to On inside web.config file under <system.web> element
<customErrors mode="On" />
2. In the FilterConfig.cs file inside App_Start folder, make sure the filters.Add(new HandleErrorAttribute()) is there.public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
}
3. Edit Error.cshtml inside \Views\Shared folder to show error message
@model System.Web.Mvc.HandleErrorInfo
@{
ViewBag.Title = "Error";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div>An error has occurred</div>
@if (Model != null && HttpContext.Current.IsDebuggingEnabled)
{
<div>
<p>
<b>Exception:</b> @Model.Exception.Message<br />
<b>Controller:</b> @Model.ControllerName<br />
<b>Action:</b> @Model.ActionName
</p>
<div style="min-height: 400px;overflow:auto">
<pre>
@Model.Exception.StackTrace
</pre>
</div>
</div>
}
else
{
<p>@Model.Exception.Message</p>
}
4. The system will show the Error page in the following conditions:
1) When there is uncaught Exceptions, the system will automatically call this Error view and pass the Exception information
2) You can manually redirect to this view in your error handling code:
protected ActionResult GotoErrorPage(string strErrorMessage)
{
string controller = this.ControllerContext.RouteData.Values["controller"].ToString();
string action = this.ControllerContext.RouteData.Values["action"].ToString();
return View("Error", new HandleErrorInfo(new System.Exception(strErrorMessage)), controller, action));
}
5. If you want to create your own customized error page for HTTP status code 401, 404 etc, you need add more <error redirect /> configuration inside <customErrors> element inside web.config file. However, you need to create your own ErrorController and error views to handle different situation.
Comments