朋友们!我正在尝试添加ASP.NET Core 2.0 Web API版本控制(Microsoft.Asp.NetCore.Mvc.Versioning包)和本地化(Microsoft.AspNetCore.Localization)以获取URL,如: 的 “API / {培养= EN-US} / V {版本:apiVersion} / [控制器]”
另外,他们工作,但一起不断错误404 ..(
[ApiVersion("1")]
[Route("api/{culture=en-US}/v{version:apiVersion}/[controller]")]
public class ValuesController : Controller
{
private readonly IStringLocalizer<ValuesController> _localizer;
public ValuesController(IStringLocalizer<ValuesController> localizer)
{
_localizer = localizer;
}
[HttpGet]
public IActionResult Get()
{
var result = $"{_localizer["Title"].Value}";
return Ok(result);
}
}
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddLocalization(x => x.ResourcesPath = "Resources");
services.AddRouteAnalyzer();
services.AddApiVersioning(opt => {
opt.ReportApiVersions = true;
opt.AssumeDefaultVersionWhenUnspecified = true;
opt.DefaultApiVersion = new ApiVersion(1, 0);
});
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouter(routes =>
{
routes.MapMiddlewareRoute("api/{culture=en}/{*mvcRoute}", subApp =>
{
var supportedCultures = new[]
{
new CultureInfo("en"),
new CultureInfo("ru"),
};
var localizationOptions = new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
};
var requestProvider = new RouteDataRequestCultureProvider();
localizationOptions.RequestCultureProviders.Insert(0, requestProvider);
subApp.UseRequestLocalization(localizationOptions);
subApp.UseMvcWithDefaultRoute();
});
});
}
}