C
C#3mo ago
hutoanhill

Temporary Pause

I am using .NET 8 I've got an ASP.NET API that i would like to pause. The goal of this is to prevent any endpoints from being called for a short amount of time. I could do this procedurally with a constant variable that's checked, but I would rather not send nothing. Is there some kind of HTML code i could return that would indicate that the API is offline but like on purpose? Instead of just returning nothing. I also need a way to return this response to the user when they try to access the API while its paused. Is there a way for be to disable all the endpoints and run a default endpoint?
4 Replies
Angius
Angius3mo ago
I'd do it via a middleware If API access is paused, return the info about that If it's not, continue As for the status code... 503 Service Unavailable maybe? https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/503
hutoanhill
hutoanhill3mo ago
Great. How do i add that code to the response? atm i just am returning a result from a method if registered with a WebApplication.MapGet()(or the relivent MapSomthing). How do i manipulate the HTTP responce?
Angius
Angius3mo ago
As I said, easiest way to handle it would be a middleware Some
app.Use((ctx, next) => {
if (paused)
{
ctx.Response.StatusCode = 503;
return Task.CompletedTask;
}
return next(ctx);
});
app.Use((ctx, next) => {
if (paused)
{
ctx.Response.StatusCode = 503;
return Task.CompletedTask;
}
return next(ctx);
});
hutoanhill
hutoanhill3mo ago
thats perfict. thanks!
Want results from more Discord servers?
Add your server