Search code examples
c#odata.net-5

OData unbound function with a string parameter does not seem to work - OData DotNetCore


I have been looking into OData for .NETCORE for the first time but can't seem to achieve something quite simple.

I have created an unbound function which returns a string and accepts a string parameter, I have read multiple docs and copied exactly what they did there (except, I replaced the int parameter for a string).

So I ended up with the following setup (I am using .NET 5 / API):

 app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
    endpoints.MapRazorPages();
    endpoints.MapHub<DataHub>("/dataHub"); // map hub enpoint
    endpoints.MapODataRoute("api", "api", GetEdmModel()); // oData endpoints
});

private IEdmModel GetEdmModel()
{
    var builder = new ODataConventionModelBuilder();

    builder.Function("GetClients").Returns<string>().Parameter<string>("UserId");

    return builder.GetEdmModel();
}

ApiController: (but the Controller should not matter since it is a unbound function)

[HttpGet]
[ODataRoute("GetClients(UserId={UserId})")]
public string GetClients([FromODataUri] string UserId)
{
    return string.Empty;
}

Endpoints which I tried:

  1. localhost/GetClients(UserId=abc)
  2. localhost/api//GetClients(UserId=abc)

When I replace the string with a int, for some reason, it does work..


Solution

  • String parameters have to be provided in single quotes:

    localhost/api/GetClients(UserId='abc')