The method of using function switch to Control Route access in ASP.NET Core
Today, I would like to share with you the relevant knowledge of ASP.NET Core using function switches to control routing access. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
1. Function switch
Function switch (Feature flags) is such a deployment technology that helps to improve the flexibility of applications.
Using function switches, you can deploy new features to a production environment, but limit their availability.
By using the switch, you can activate a new feature to control whether or not to restart the application or deploy new code.
They separate the release of new features from code deployment.
3. Realize
First, we need to reference the nuget package Microsoft.FeatureManagement.AspNetCore.
Then, modify the Startup.cs code:
Public void ConfigureServices (IServiceCollection services) {... Services.AddFeatureManagement ();...}
Next, add a configuration:
FeatureManagement: {"ForbiddenDebugEndpoint": false} 3. Use
Modify the DebugMiddleware we implemented last time:
Public class DebugMiddleware: IMiddleware {private readonly IFeatureManager _ featureManager; public DebugMiddleware (IFeatureManager featureManager) {_ featureManager = featureManager;} public async Task InvokeAsync (HttpContext context, RequestDelegate next) {var isDebugEndpoint = context.Request.Path.Value.Contains ("/ test"); var debugEndpoint = await _ featureManager.IsEnabledAsync ("ForbiddenDebugEndpoint") If (isDebugEndpoint & & debugEndpoint) {context.SetEndpoint (new Endpoint ((context) = > {context.Response.StatusCode = StatusCodes.Status403Forbidden; return Task.CompletedTask;}, EndpointMetadataCollection.Empty, "No access");} await next (context) }}
The key is this sentence, we used the function switch:
Var debugEndpoint = await _ featureManager.IsEnabledAsync ("ForbiddenDebugEndpoint"). Conclusion:
After running, we found that we only need to modify the configuration without restarting the program, and we can control whether the route can be accessed:
These are all the contents of the article "how ASP.NET Core uses function switches to control routing access". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.