Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The URLRewrite middleware in .NET Core can rely on Config dependency injection/scoped service by registering it as a scoped service in the Startup.cs file.

First, a new class that implements the IUrlRewriter interface needs to be created. This class will contain the logic for the URL rewriting.

Next, the class needs to be registered in the ConfigureServices method in the Startup.cs file as a scoped service using the AddScoped extension method.

public void ConfigureServices(IServiceCollection services) {  // Register the UrlRewriter as a scoped service  services.AddScoped<IUrlRewriter, UrlRewriter>(); } 

Once registered, the middleware can then be added to the pipeline in the Configure method, and it will have access to the Config dependency injection/scoped service.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {  // Add the UrlRewrite middleware to the pipeline  app.UseMiddleware<UrlRewriteMiddleware>(); }