【发布时间】:2020-01-20 15:58:33
【问题描述】:
下午好,
我在使用属性路由和 ASP.NET 核心路由中间件的 Web API 中的端点路由方面遇到了一些问题。
我有一个大致如下所示的 API 控制器:
public class UsersController : ControllerBase
{
[HttpGet]
[Route("v1/users/{id}", Name = nameof(GetUser), Order = 1)]
public async Task<ActionResult> GetUser([FromQuery(Name = "id")] string userGuid)
{
// Implementation omitted.
}
[HttpGet]
[Route("v1/users/me", Name = nameof(GetCurrentUser), Order = 0)]
public async Task<ActionResult> GetCurrentUser()
{
// Implementation omitted.
}
}
我正在尝试配置端点路由,以便将“v1/users/me”的请求路由到“GetCurrentUser()”方法,同时请求匹配模板“v1/users/{id}”(其中 { id} != me) 被路由到 'GetUser()' 方法。我希望可以通过将“v1/users/me”端点放在端点顺序中的另一个端点之前来解决这个问题,但路由中间件似乎不尊重 order 参数。在映射其余端点之前,我还尝试显式映射“v1/users/me”端点,但这似乎也不起作用。
这是当前的启动配置:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseResponseCompression();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
// Explicit mapping of the GetCurrentUser endpoint - doesn't seem to do anything.
// endpoints.MapControllerRoute("v1/users/me", "Users/GetCurrentUser");
endpoints.MapControllers();
}
}
这是否可以通过属性路由来实现,如果可以,我缺少什么?
谢谢!
【问题讨论】:
-
id 是 int 还是可以是任何字符串?
-
需要一个字符串编码的 GUID,但它可以是任何字符串。
标签: c# asp.net-web-api-routing asp.net-core-3.1