How do i use Application_Error in ASP.NET MVC?

前端 未结 2 751
粉色の甜心
粉色の甜心 2020-12-23 12:32

I want to use Application_Error with my MVC project, but i can\'t get it to work. I add the following to my Global.asax file:

    protected void Application_         


        
2条回答
  •  猫巷女王i
    2020-12-23 13:09

    • Do you have a Session set up in the first place? If the error is triggered from an IHttpHandler not marked with IRequiresSessionState, then accessing Session will fail.
    • What are you doing with Session["Test"]? Are you sure your code is actually not working? You could try a File.Open and simply output some text (say, the current time) to C:\my-log.txt, which is slightly more likely to succeed than using Session.
    • GetBaseException isn't useful in this case (nor generally for logging) as far as I can tell.
    • Message is of type string - calling .ToString() isn't necessary. In general, I'd strongly recommend avoiding ToString() where possible - if you're using it because you're unsure of the type of the object, that should be a red flag; doing an end-run around the type system can hide subtle bugs (for instance, DBNull.Value.ToString() == ""). For GUI's the builtin types provide a .ToString(IFormatProvider) overload which is culture-sensitive and avoid portability issues. Since that overload is also not present on object it's also a safeguard to avoid the very weakly typed .ToString calls.

提交回复
热议问题