I'm developing ASP.NET MVC application, for handling error, I followed this tutorial. So, code in the FilterConfig.cs:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
    filters.Add(new CustomHandleErrorAttribute());
}

I added a ErrorController in Controllers folder:

public class ErrorController : Controller
{
    // GET: Error
    public ActionResult Index()
    {
        return View();
    }
}

I also created the View to display the error in Views/Error:

@model System.Web.Mvc.HandleErrorInfo

@{
    ViewBag.Title = "Index";
}

<h1 class="text-danger">Custom Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>

<h2>Exception details</h2>
<p>
    Controller: @Model.ControllerName <br />
    Action: @Model.ActionName<br />
    Message: @Model.Exception.Message <br />
    StackTrace: <br />
</p>
<pre>Exception: @Model.Exception.StackTrace</pre>

Then I created Admin Area and moved all the admin's views to Admin folder. Admin Area has its layout, it's different with main layout.

For handling error in Admin Area, I also created ErrorController.cs in ~/Areas/Admin/Controllers and its View in ~/Areas/Admin/Views/Error to show error in admin's layout, but when the error occur on admin's page. It will display the message in main layout (~/Views/Error)

How to handle error for each areas in MVC?

Related posts

Recent Viewed