ASP.NET Web API(.NET Core 3.1)中的终结点路由问题]]

问题描述 投票:1回答:2

下午好,

我在使用属性路由和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();
   }
}

使用属性路由是否可以实现,如果可以,我缺少什么?

谢谢!

[下午好,我在使用属性路由和ASP.NET核心路由中间件的Web API中进行端点路由时遇到了一些麻烦。我有一个大致类似于...

c# asp.net-web-api-routing asp.net-core-3.1
2个回答
0
投票

如果您只保留默认值,这应该已经可以正常工作:


0
投票

问题出在API端点方法的注释上。

© www.soinside.com 2019 - 2024. All rights reserved.