Incorrect Routing
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 505
- Forks
- 186
- PR merge metrics
- No merged PRs in 30d
Description
I am using a combination of ApiController and OData in my project. My controllers have multiple GET actions. One action expects an ID and returns a single object. Another action does not expect IDs, returns a queryable, and is to be used with OData. I've set everything up right, but OData seems to randomly chose which of the GET actions it wants to use (despite only one of the methods having an EnableQuery attribute and only one of the methods being an IQueryable). What's weird is that the action that OData picks is the same until I make changes in my controller (for example, add another action which is completely unrelated).
Given below is my setup:
Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers(options =>
{
options.Filters.Add(typeof(UnitOfWorkCompleteFilter));
})
.AddOData(opt => opt.AddRouteComponents("Odata", GetEdmModel()).Filter().Select().Expand().OrderBy().Count().SetMaxTop(100));
}
private static IEdmModel GetEdmModel()
{
ODataConventionModelBuilder builder = new();
builder.EnableLowerCamelCase();
builder.EntitySet<LocaleVM>("Locale");
return builder.GetEdmModel();
}
LocaleController.cs:
[Authorize]
[Route("api/[controller]")]
[ApiController]
public class LocaleController : Controller
{
private readonly ILocaleService _localeService;
private readonly ILogger<LocaleController> _log;
public LocaleController(ILocaleService localeService,
ILogger<LocaleController> log)
{
_localeService = localeService;
_log = log;
}
[HttpGet("{id}")]
public async Task<LocaleVM> Get(Guid id)
{
return await _localeService.Get(id);
}
[HttpGet]
[EnableQuery]
[AllowAnonymous]
public async Task<IQueryable<LocaleVM>> Get()
{
return await _localeService.GetAll();
}
}
Why would OData queries even hit the GET Action which requires a Guid?
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with Startup.cs, especially the OData route components and EDM model, then inspect the two GET actions and their attributes in LocaleController.cs. Reproduce an OData query against the Locale routes and trace which action is selected; done means the routing behavior is explained and the conditions causing the Guid action to be chosen are isolated.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100