Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

Global Exception handling not working in Web API 2.2

$
0
0

I have a WebForms website project in .Net 4.5 to which I have added ASP.Net Web API 2.2 NuGet package. The Web API methods are being called successfully and returning expected values.

However, after I created a global exception handler class for Web API 2.2, and changed the controller class name from ProductController to Product to force the following error, then this error was not getting caught by global exception handler.

{"Message":"No HTTP resource was found that matches the request URI 'http://localhost/mysite/Vendors/api/
 Product/SaveProductName'.","MessageDetail":"No type was found that matches the controller named 'Product'."}

My question: Why is the global exception handler not catching the above error, but catching exceptions thrown in the Web API method when the Web API method is found successfully i.e. ProductController class name is not changed to deliberately cause an exception?

Global Exception Handler as below

public class GlobalExceptionLogger : ExceptionLogger
{
   public GlobalExceptionLogger()
   {

   }

  public override void Log(ExceptionLoggerContext context)
  {
    if (context != null && context.Exception != null)
    { 
          MyLogging.Log( context.Exception );
    }
    base.Log(context);
  }
}

 Web API controller class (for the error not being caught by global exception handler I changed the class name to Product)

public class ProductController: ApiController
{
        //Methods omitted to SAVE SPACE
[HttpPut]
        [Authorize]
        public HttpResponseMessage SaveProductName(ProdName pn)
        {
            string x = null;
            string y = x.Trim();//This null reference exception is being caught by global exception handler
//remaining code omitted
} }

 


Viewing all articles
Browse latest Browse all 4850

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>