您好,我有一个控制器,我想对其路由进行版本设置。但是,我不想将api版本添加到querystring中,而是添加到url的开头,在控制器名称之后:
典型路径
/api/admin/get-computer/1
版本化路径
:
/api/admin/V[version]/get-computer/1
我不知道如何配置控制器和
UseMvc
从扩展
Startup
[ApiVersion("1.0")]
[ApiVersion("2.0")]
[Route("/api/admin")]
public class AdminController : Controller {
[HttpGet]
[MapToApiVersion("1.0")]
[Route("/get-computer")]
public async Task<ActionResult<Catalog>> GetComputerAsync1(int id) {
return null;
}
[HttpGet]
[MapToApiVersion("2.0")]
[Route("/get-computer")]
public async Task<ActionResult<Catalog>> GetCatalogAsync2(int id) {
return null;
}
}
启动
public void Configure(IApplicationBuilder app) {
app.UseCors(x => x.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());
app.UseMvc(routes => {
routes.MapRoute(
name: "default",
template: "{controller=Admin}/V[???]/{action=Index}/{id?}");
});
}
首选用法
"/api/admin/V1/get-computer/3"
"/api/admin/V2/get-computer/4"
我怎样才能做到这一点?中的路由模板的假定形式是什么
使用MVC