OData / OData/AspNetCoreOData

Requestind individual properties

Open
#116 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Hi,

I am having problems requesting individual properties of an entity.

POCO

    public class Person
    {
        public string Identifier { get;set; }
        public string Name { get; set; }
        public string Surname { get; set; }
    }

Startup

public class Startup {
  public IConfiguration Configuration {
    get;
  }

  public Startup(IConfiguration configuration) {
    Configuration = configuration;
  }

  // This method gets called by the runtime. Use this method to add services to the container.
  public void ConfigureServices(IServiceCollection services) {
    //Persistence Injection
    services.AddPersistenceDependencyServices(this.Configuration.GetConnectionString("QueryDB"));
    services.AddDependencyInjections();

    services.AddOData();
    services.AddControllers();
  }

  // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
    if (env.IsDevelopment()) {
      app.UseDeveloperExceptionPage();
    }

    app.UseRouting();

    app.UseEndpoints(endpoints => {
      endpoints.MapODataRoute("TestAPIv1", "TestAPI/v1", GenerateEdmModel());
      endpoints.Select().Expand().OrderBy().Filter().Count().MaxTop(null);
      endpoints.EnableDependencyInjection();
      endpoints.SetTimeZoneInfo(TimeZoneInfo.Utc);
    });
  }

   private IEdmModel GenerateEdmModel() {

      var edmBuilder = new ODataConventionModelBuilder();
      edmBuilder.EntitySet<Person>("Persons");
      edmBuilder.EntityType<Person>().HasKey(p => p.Identifier);         
  }
}

PersonsController

  public class PersonsController : ODataController
    {
        private readonly IPersonService personService;

        public PersonsController(IPersonService personService)
        {
            this.personService= personService;
        }

        public IActionResult Post([FromBody] T entity)
        {
            this.personService.Create(entity);
            return Created(entity);
        }

        [ODataRoute]
        [EnableQuery]
        public IQueryable<T> Get() => this.personService.GetAll();

        [ODataRoute]
        [EnableQuery]
        public SingleResult<T> Get([FromODataUri] string key)
        {
                   IQueryable<T> entities = this.personService.FindByEntityId(key);
                   return SingleResult.Create(entities);
        }      
    }

Now, if I request the collection it works fine:

http://host/testApi/v1/Persons

But if I request some individual property, I get a 404 Not found.

http://host/testApi/v1/Persons('1')/Name

Is this a bug, or is something wrong?

OData: Microsoft.AspNetCore.OData 7.5.6
Target framework: .NET Core 3.1
Operating system: Windows 10
IDE: Visual Studio 2019 16.4.0

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.

Research direction

Start with the Startup OData route configuration and the PersonsController actions, then reproduce the collection request and the property URL /Persons('1')/Name on Microsoft.AspNetCore.OData 7.5.6. Compare the documented routing requirements for individual-property requests with this controller and model setup; done means the property request returns the expected value rather than 404.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.