Hello,
I have an asp.net web API. The client wants me to add a valid time interval (09:00 - 21:30) in order to accept requests. Let's say if a request comes at 08:00, it will be denied with a message saying Requests are permitted between 9:00 AM and 21:30 PM. I am planning to implement this logic in the global.asax as follows, but I am not sure if there is a better way.
What if the client wants to change the time interval? I need to change it in the code base and deploy it to the server. So I think I need to put those times into an appsettings of config. But it still has a cost, when I change it on the config, the IIS will be restarted right? So keeping those times in the database seems to be the only solution? What are your suggestions?
I have an asp.net web API. The client wants me to add a valid time interval (09:00 - 21:30) in order to accept requests. Let's say if a request comes at 08:00, it will be denied with a message saying Requests are permitted between 9:00 AM and 21:30 PM. I am planning to implement this logic in the global.asax as follows, but I am not sure if there is a better way.
C#:
ublic class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
}
protected void Application_BeginRequest()
{
if (CheckTime9To2130())
{
//do something
}
else
{
Response.StatusCode = 429;
Response.Write("Requests are permitted between 9:00 AM and 21:30 PM");
Response.End();
}
}
private bool CheckTime9To2130()
{
var h = DateTime.Now.TimeOfDay.Hours;
var m = DateTime.Now.TimeOfDay.Minutes;
if ((9 <= h && h <= 21) || (h == 21 && m <= 30))
{
return true;
}
return false;
}
}
What if the client wants to change the time interval? I need to change it in the code base and deploy it to the server. So I think I need to put those times into an appsettings of config. But it still has a cost, when I change it on the config, the IIS will be restarted right? So keeping those times in the database seems to be the only solution? What are your suggestions?