OData / OData/AspNetCoreOData

Cannot use single endpoint using Route attribute and with AddRouteComponents

Open
#851 6 comments 0 reactions 1 assignee View on GitHub

@xuzhg is already working on this.

Since Feb 27, 2023.

followup
Dominant language
C#
Stars
505
Forks
186
PR merge metrics
No merged PRs in 30d

Description

Assemblies affected
Microsoft.AspNetCore.OData 8.0.12

Describe the bug
I have a controller that has 4 actions: CRUD. I want to use OData only on Read. My controller should fit api/[controller] template.
I was not able to do that with OData. It is opening two endpoints or showing some errors

Reproduce steps
My controller:

[Route("api/[controller]")]
    public class CustomersController : ControllerBase
    {
        private static Random random = new Random();
        private static List<Customer> customers = new List<Customer>(
            Enumerable.Range(1, 3).Select(idx => new Customer
            {
                Id = idx,
                Name = $"Customer {idx}",
                Orders = new List<Order>(
                    Enumerable.Range(1, 2).Select(dx => new Order
                    {
                        Id = (idx - 1) * 2 + dx,
                        Amount = random.Next(1, 9) * 10
                    }))
            }));

        [HttpGet()]
        [EnableQuery]
        public ActionResult Get()
        {
            return Ok(customers);
        }

        [HttpPut()]
        public ActionResult Put()
        {
            return Ok();
        }

        [HttpPost()]
        public ActionResult Post()
        {
            return Ok();
        }

        [HttpDelete]
        public ActionResult Delete()
        {
            return Ok();
        }
    }

I am using AddRouteComponents("api", modelBuilder.GetEdmModel())), because my controller has api prefix

It is throwing an error:

Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException: The request matched multiple endpoints. Matches: 

WebApplication3.Controllers.CustomersController.Get (WebApplication3)
WebApplication3.Controllers.CustomersController.Get (WebApplication3)
   at Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ReportAmbiguity(CandidateState[] candidateState)
   at Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ProcessFinalCandidates(HttpContext httpContext, CandidateState[] candidateState)
   at Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.SelectAsync(HttpContext httpContext, CandidateSet candidateSet)
   at Microsoft.AspNetCore.Routing.Matching.DfaMatcher.SelectEndpointWithPoliciesAsync(HttpContext httpContext, IEndpointSelectorPolicy[] policies, CandidateSet candidateSet)
   at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.<Invoke>g__AwaitMatch|8_1(EndpointRoutingMiddleware middleware, HttpContext httpContext, Task matchTask)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

Expected behavior
It should use endpoint without an error

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.