Error handling is the main concern in any application, whether it is web application or desktop application.
HandleError Attribute
This filter works only when custom errors are enabled in the Web.config file. You can enable custom errors by adding a customErrors attribute inside the tag, as shown below:
...
HandleError Attribute at Action Method Level
Simply put this attribute to the action method to tell MVC how to react when an exception occurs.
[HandleError(ExceptionType = typeof(System.Data.DataException), View = "DatabaseError")]
public ActionResult Index(int id)
{
var db = new MyDataContext();
return View("Index", db.Categories.Single(x => x.Id == id));
}
HandleError Attribute at Controller Level
Simply put this attribute to the controller to tell MVC how to react when an exception occurs with in the methods of this controller.
[HandleError(ExceptionType = typeof(System.Data.DataException), View = "DatabaseError")]
public class HomeController : Controller
{
/* Controller Actions with HandleError applied to them */
}
0 Comment(s)