Custom error pages on asp.net MVC3

前端 未结 6 1088
无人及你
无人及你 2020-11-22 11:07

I\'m developing a MVC3 base website and I am looking for a solution for handling errors and Render custom Views for each kind of error. So imagine that I have a \"Error\" Co

6条回答
  •  借酒劲吻你
    2020-11-22 11:47

    I see you added a config value for EnableCustomErrorPage and you're also checking IsDebuggingEnabled to determine whether or not to run your error handling.

    Since there's already a configuration in ASP.NET (which is meant exactly for this purpose) it's easiest to just say :

        protected void Application_Error()
        {
            if (HttpContext.Current == null) 
            {
                    // errors in Application_Start will end up here                
            }
            else if (HttpContext.Current.IsCustomErrorEnabled)
            {
                    // custom exception handling
            }
        }
    

    Then in the config you'd put which is safe to deploy like that, and when you need to test your custom error page you'd set it to so you can verify that it works.

    Note you also need to check if HttpContext.Current is null because an exception in Application_Start will still his this method although there won't be an active context.

提交回复
热议问题