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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 48
    Comment on it

    If you want to handle exception at the controller level you will create a class for that inside the controller that will override the OnException method to log errors

    @model System.Web.Mvc.HandleErrorInfo
    @{
    Layout = null;
    }
     
    <!DOCTYPE html>
    <html>
    <head>
       <meta name="viewport" content="width=device-width" />
       <title>Error</title>
    </head>
    <body>
    <h2>
        Sorry, an error occurred while processing your request.
    
    </h2>
        <h2>Exception details</h2>
    <p>
        Controller: @Model.ControllerName <br>
        Action: @Model.ActionName
        Exception: @Model.Exception
    </p>
      
    </body>
    </html>

    This is the view that will show the error in detail it is strongly typed

    You need to set the custom error mode on for error logging as per as your requirement

    <customErrors mode="On"/>

     

    After doing that where ever the exception occurs the detail page gets showed

    using System;
    using System.Data.Common;
    using System.Web.Mvc;
    
    namespace ExceptionHandlingMVC.Controllers
    {
        [HandleError]
        public class ExceptionHandlingController : Controller
        {
            public ActionResult TestMethod()
            {
                throw new Exception("Test Exception");
                return View();
            }
        }
    }

     

    .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: