Metadata of Endpoints
Some, not all, routes need a daily downtime between 17 hrs to 18 hrs. So they have to be shut during that period so that no requests are processed. Any request has to be redirected to another page or a website, say, hoven.in. How should that be implemented?
Solution
Keep the visual studio template as it is. Edit the Startup class like this
public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddMvc(); } public void Configure(IApplicationBuilder app) { app.UseRouting(); // insert a middleware between // UseRouting and UseEndpoints app.Use(next => context => { // get the endpoint var endPoint = context.GetEndpoint(); // if it exists if (null != endPoint) { // obtain the Limited attribute var limAttr = endPoint.Metadata.GetMetadata<LimitedAttribute>(); // if it is not serving, redirect if ((null != limAttr) && limAttr.IsServing) { context.Response.Redirect("http://hoven.in"); // and short circuit return Task.CompletedTask; } } // pass to the next component return next(context); }); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute ( pattern: "/{controller}/{action}/{id?}", defaults: new { controller = "Hoven", action = "Index" }, constraints: new { id = "int" }, name: "MyRoute" ); }); } } [AttributeUsage(AttributeTargets.Method)] public class LimitedAttribute : Attribute { public bool IsServing { get; private set; } public LimitedAttribute() { int hour = DateTime.UtcNow.Hour; IsServing = (hour < 17) || (hour > 18); } } public class HovenController : Controller { [Limited] public IActionResult Index(int? id) { return new ObjectResult("Hello, MVC!"); } }
Comments and Discussion
Please write your comments and discussion on the following youtube video itself:
This Blog Post/Article "Metadata of Endpoints in ASPNET Core" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.