Home » c# » c# web api 2 – any way to wrap all routes for “site is down”?

c# web api 2 – any way to wrap all routes for “site is down”?

Posted by: admin October 27, 2017 Leave a comment

Questions:

The high level problem I’m trying to solve:

Given our design of
– c# web api to modify entities and retrieve view models
– react SPA frontend

I want to be able to conditionally turn the c# web api “off” and tell the SPA the “site is down for maintenance”. The purpose for this is to prevent odd errors in the client in the case someone is using the site during the transition.

The initial idea I had to solve this was to wrap each call in a web.config check to see if the site is down, and if so, return a hard-coded error response which the frontend knows what to do with. However the cleanest approach I can think of is to inject a service into each web api controller and invoke it at the beginning of each route. I was instead hoping for a way to avoid this boilerplate.

In node I would decorate all the routes in a wrapper function that does this. C# is a little more foreign to me.

Answers:

If you host your WebAPI as its own ASP.NET site then you can place an app_offline.htm file in the IIS root, and it will automatically go into maintenance mode.

ASP.NET will immediately server 503’s and the page contents for all new requests, while allowing existing requests to complete.

Leave a Reply

Your email address will not be published. Required fields are marked *