Global exception handling in OWIN middleware

后端 未结 2 1513
陌清茗
陌清茗 2020-12-07 16:50

I\'m trying to create a unified error handling/reporting in ASP.NET Web API 2.1 Project built on top of OWIN middleware (IIS HOST using Owin.Host.SystemWeb). Currently I use

2条回答
  •  旧巷少年郎
    2020-12-07 17:26

    There are a few ways to do what you want:

    1. Create middleware that is registered first, then all exceptions will bubble up to that middleware. At this point just write out your JSON out via the Response object via the OWIN context.

    2. You can also create a wrapping middleware which wraps the Oauth middleware. In this case it will on capture errors originating from this specific code path.

    Ultimately writing your JSON message is about creating it, serializing it, and writing it to the Response via the OWIN context.

    It seems like you are on the right path with #1. Hope this helps, and good luck :)

提交回复
热议问题