Ask Your Question
2

How can the URLRewrite middleware in .Net Core rely on Config dependency injection/scoped service?

asked 2023-05-15 13:04:56 +0000

huitzilopochtli gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-05-15 13:13:02 +0000

lakamha gravatar image

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>(); } 
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-15 13:04:56 +0000

Seen: 10 times

Last updated: May 15 '23