Tired of looking for errors in log files? Use OneTrueError - Automatic exception management for .NET.

How to handle errors in ASP.NET MVC

There are several blog posts regarding error handling for ASP.NET MVC. They use everything from Application_Error to exception handling in the base controller. With this post I’ll show how you can use the built in features in MVC to treat errors.

Update Try my new .NET service for handling errors. The basic subscription is free. Read more about the ASP.NET handling features in it. You just need two lines of code to handle ALL uncaught errors in ASP.NET applications.
 

MVC got an attribute called [HandleError] which you should set on your BaseController (or on each controller). There is no need to specify any of the options for the attribute.

The problem with [HandleError] is that it can’t handle 404 (not found), thus we need to create a custom error controller and tell ASP.NET to use it (by configuring web.config and creating and ErrorController):

<customErrors mode="RemoteOnly" defaultRedirect="~/Error/">
  <error statusCode="404" redirect="~/Error/NotFound/" />
</customErrors>

The error controller

public class ErrorController : BaseController
{
	public ActionResult NotFound(string url)
	{
		var originalUri = url ?? Request.QueryString["aspxerrorpath"] ?? Request.Url.OriginalString;

		var controllerName = (string)RouteData.Values["controller"];
		var actionName = (string)RouteData.Values["action"];
		var model = new NotFoundModel(new HttpException(404, "Failed to find page"), controllerName, actionName)
		{
			RequestedUrl = originalUri,
			ReferrerUrl = Request.UrlReferrer == null ? "" : Request.UrlReferrer.OriginalString
		};

		Response.StatusCode = 404;
		return View("NotFound", model);
	}

	protected override void HandleUnknownAction(string actionName)
	{
		var name = GetViewName(ControllerContext, "~/Views/Error/{0}.cshtml".FormatWith(actionName),
													"~/Views/Error/Error.cshtml",
													"~/Views/Error/General.cshtml",
													"~/Views/Shared/Error.cshtml");

		var controllerName = (string)RouteData.Values["controller"];
		var model = new HandleErrorInfo(Server.GetLastError(), controllerName, actionName);
		var result = new ViewResult
		{
			ViewName = name,
			ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
		};


		Response.StatusCode = 501;
		result.ExecuteResult(ControllerContext);
	}

	protected string GetViewName(ControllerContext context, params string[] names)
	{
		foreach (var name in names)
		{
			var result = ViewEngines.Engines.FindView(ControllerContext, name, null);
			if (result.View != null)
				return name;
		}
		return null;
	}
}

NotFound action

The notfound action builds information about the route and where the user came from. You might want to log that information. The model that it uses derives the built in error view model (HandleErrorInfo) and looks like this:

public class NotFoundModel : HandleErrorInfo
{
	public NotFoundModel(Exception exception, string controllerName, string actionName)
	: base(exception, controllerName, actionName)
	{
	}
	public string RequestedUrl { get; set; }
	public string ReferrerUrl { get; set; }
}

Which is used by the following view:

@using YourApplication.Models.Errors
@model NotFoundModel
@{
    ViewBag.Title = "Page could not be found";
}
<h2>@ViewBag.Title</h2>
<div>
You tried to visit '@Model.RequestedUrl' which cannot be found.
</div>

All other errors

The HandleUnknownAction method is used to handle all other error codes which [HandleError] can’t handle (it will be invoked since no other action methods exists). Look at the code for it and you’ll discover that it tries to find a customized view for each HTTP error.

Handling errors in your POST actions

Another important aspect is how you treat errors in your POST actions. Here is a sample method which is taking advantage of the built in model validation.

[HttpPost]
public virtual ActionResult Create(YourModel model)
{
	if (!ModelState.IsValid)
		return View(model);

	try
	{
		var dbEntity = _repository.Get(model.Id);
		Mapper.Map(model, dbEntity);
		_repository.Save(dbEntity);
		return RedirectToAction("Details", new { id = model.Id });
	}
	catch (Exception ex)
	{
		ModelState.AddModelError("", ex.Message);
		//log error here.
		return View(model);
	}
}

Keypoints:

  • Always validate model first and display any errors)
  • Fetch / Copy / Save – make sure that the view model only contains fields that can be changed)
  • Include any error in the model state (to get it in the validation summary)
  • Log all errors!
This entry was posted in CodeProject and tagged , . Bookmark the permalink.
  • keith

    very good example, however.. it doesnt work. the HandleUnknownAction can still get called even when an error does not exist. So in this case the Server.GetLastError() fails because its null.

    • http://www.gauffin.org jgauffin

      Ok. I’ll fix that tomorrow.

      • http://yassershaikh.com Yasser R Shaikh

        Is that fixed ? I am about to use it now…

  • willy

    excelent example !!! …but when I throw an exception the HandleUnknownAction fails, because Server.GetLastError() is null …any advice on how to catch HttpException, ApplicationException and so on ? Thks …

  • siddharth

    how to get exception code instead of exception message

    • http://www.gauffin.org jgauffin

      where?

  • Forty-Two

    Implementing something very similar. Trying to pass in the url to the Error method as you have above but url parameter is always null. Any thoughts as to what I may be missing?

  • http://twitter.com/The_Mark_Perry Mark Perry

    .FormatWith.. i’m getting an error on that?

  • Andrew