Good afternoon,
I'm having some trouble with endpoint routing in my web API using attribute routing and the ASP.NET core routing middleware.
I have an API controller that looks roughly like the following:
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.
}
}
I am trying to configure the endpoint routing so that requests for 'v1/users/me' are routed to the 'GetCurrentUser()' method, while requests matching the template 'v1/users/{id}' (where {id} != me) are routed to the 'GetUser()' method. I was hoping that this could be solved by placing the 'v1/users/me' endpoint before the other endpoint in the endpoint order, but it seems the order parameter isn't respected by the routing middleware. I've also tried explicitly mapping the 'v1/users/me' endpoint before mapping the remaining endpoints, but this doesn't seem to work either.
Here is the current startup configuration:
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();
}
}
Is this possible to achieve with attribute routing, and if so, what am I missing?
Thanks!