Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Error Handling and Logging in MVC

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 182
    Comment on it

    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 */
        }
    
    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: