简体   繁体   English

ASP.NET 核心 Web API:将应用设置值注入 Z594C103F2C6E04C3C3DAZAB059F031 路由

[英]ASP.NET Core Web API: inject app setting value into controller route

I have an ASP.NET Core Web API project, and I would like my controllers' routes to be:我有一个 ASP.NET 核心 Web API 项目,我希望我的控制器的路线是:

api/vX.Y/custom_name

I would have the second value in AppSettings, for example例如,我会在 AppSettings 中有第二个值

"ApiVersion":"vX.Y"

but I'm not sure how to "inject" this value into the controller route.但我不确定如何将此值“注入”到 controller 路由中。

If you want to enable default api version from appsettings.json , you could try to follow:如果您想从 appsettings.json 启用默认appsettings.json版本,您可以尝试遵循:

  1. appsettings.json

     { "ApiVersion": "2.1" }
  2. ConfigureApiVersioningOptions

     public class ConfigureApiVersioningOptions: IConfigureOptions<ApiVersioningOptions> { private readonly IServiceProvider _serviceProvider; public ConfigureApiVersioningOptions(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; } public void Configure(ApiVersioningOptions options) { var apiVersion = _serviceProvider.GetRequiredService<IConfiguration>().GetSection("ApiVersion").Value; options.DefaultApiVersion = ApiVersion.Parse(apiVersion); } }
  3. Startup.cs

     public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddMvc(options => { options.EnableEndpointRouting = false; }); services.AddApiVersioning(); services.AddSingleton<IConfigureOptions<ApiVersioningOptions>, ConfigureApiVersioningOptions>(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseMvc(); } }
  4. ValuesController

     [ApiController] [Route("api/v{version:apiVersion}/Values")] public class ValuesController: Controller { // GET api/values [HttpGet] public IEnumerable<string> Get() { return new string[] { "value113", "value223" }; } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AuthenticationStateProvider 无法注入 ASP.NET Core Web App - AuthenticationStateProvider couldn't inject in ASP.NET Core Web App 找不到ASP.NET Web API路由控制器 - ASP.NET Web API Route Controller Not Found ASP.NET CORE、Web API:没有路由与提供的值匹配 - ASP.NET CORE, Web API: No route matches the supplied values 向ASP.NET Web API Controller添加显式操作路由 - Adding an explicit action route to ASP.NET Web API Controller ASP.NET Core Web API:通过查询参数路由 - ASP.NET Core Web API : route by query parameter 将中间件附加到 ASP.NET Core Web API 中的特定路由? - Attaching middleware to a specific route in ASP.NET Core Web API? ASP.NET 内核 Web API 自定义路由不起作用 - ASP.NET Core Web API custom route not working 根据路由值(命名空间)选择ASP.NET Web API Controller - Select ASP.NET Web API Controller depending on route value (namespace) 是否可以在ASP.NET Core Web API中将路由值绑定到自定义属性的属性? - Is it possible to bind Route Value to a Custom Attribute's Property in ASP.NET Core Web API? 如何在ASP.NET Core 6.0 Web API中读取Azure App服务的应用设置 - How to read application setting of Azure App service in ASP.NET Core 6.0 Web API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM