Error Handling in asp.net mvc 3

后端 未结 5 1453
闹比i
闹比i 2020-12-08 04:32

Is there a built in or a proper way to handle errors in asp.net mvc 3?

This is what I want to do:

  1. If the application crashes, or throws an error, it go
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-08 05:02

    For Global Error Handling
    All you have to do is change the customErrors mode="On" in web.config page
    Error will be displayed through Error.cshtml resides in shared folder.

    Make sure that Error.cshtml Layout is not null.
    [It sould be something like: @{ Layout = "~/Views/Shared/_Layout.cshtml"; }
    Or remove Layout=null code block]
    A sample markup for Error.cshtml:-

    @{ Layout = "~/Views/Shared/_Layout.cshtml"; } 
    
    @model System.Web.Mvc.HandleErrorInfo
    
    
    
    
        Error
    
    
        

    Sorry, an error occurred while processing your request.

    Controller Name: @Model.ControllerName

    Action Name : @Model.ActionName

    Message: @Model.Exception.Message

    For Specific Error Handling
    Add HandleError attribute to specific action in controller class. Provide 'View' and 'ExceptionType' for that specific error.
    A sample NotImplemented Exception Handler:

    public class MyController: Controller
        {
            [HandleError(View = "NotImplErrorView", ExceptionType=typeof(NotImplementedException))]
            public ActionResult Index()
            {
                throw new NotImplementedException("This method is not implemented.");
                return View();
            }
    }
    

提交回复
热议问题