How do I let users define the URL for a controller in my ASP.NET Core library?

37 Views Asked by At

I'm writing a library for a project, and in this library, I would like to add a controller so that other APIs can make requests, but I would like that the person that is using my library can define the URL for this controller in the startup.cs file.

here is the code for the controller that will be in my library:


    public class NetworkConfigurationController : ControllerBase
    {
        protected readonly IConfiguration _configuration;

        public NetworkConfigurationController(IConfiguration configuration)
        {
            _configuration = configuration;
            
        }

        [HttpPost("Reload")]
        public virtual IActionResult Reload()
        {
            _configuration.ReloadNetworkConfigurationProvider();
            return NoContent();
        }
    }

here is what I would like that people could do:

public void ConfigureServices(IServiceCollection services)
        {

            services.AddControllers();
            
            // would make my libray controller 'NetworkConfigurationController' be accessed in the url: http://somedomain.com/UrlForThisController
            services.AddNetworkConfigurationController("UrlForThisController")
            
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "Api docs", Version = "v1" });
            });
        }

How can I make it? i'm using netstandart2.0 for the library but the targed apis would be in .netcore5

honestly i don't know how to seach for this problem.

0

There are 0 best solutions below