简体   繁体   中英

Asp.Net Core odata not recognizing Get action method in a controller

We are using Odata 7.4.1 on Asp.Net Core 3.1, when we add new controller with Get action method, it is not working.

Odata route builder:

app.UseMvc(
            routeBuilder =>
            {
                // the following will not work as expected
                // BUG: https://github.com/OData/WebApi/issues/1837
                // routeBuilder.SetDefaultODataOptions( new ODataOptions() { UrlKeyDelimiter = Parentheses } );
                routeBuilder.ServiceProvider.GetRequiredService<ODataOptions>().UrlKeyDelimiter = Parentheses;

                // global odata query options
                routeBuilder.Count();

                routeBuilder.MapVersionedODataRoutes("odata", "api/v{version:apiVersion}", modelBuilder.GetEdmModels());
            });

Model Configuration:

public class ServiceModelConfiguration : IModelConfiguration
{
    /// <summary>
    /// Applies model configurations using the provided builder for the specified API version.
    /// </summary>
    /// <param name="builder">The <see cref="ODataModelBuilder">builder</see> used to apply configurations.</param>
    /// <param name="apiVersion">The <see cref="ApiVersion">API version</see> associated with the <paramref name="builder"/>.</param>
    public void Apply(ODataModelBuilder builder, ApiVersion apiVersion)
    {
        var services = builder.EntitySet<Service>("SM").EntityType;

        services.HasKey(p => p.Id);

        if (apiVersion <= ApiVersions.V1)
        {
            // This is how we maintain backwards compatibility without having to explicitly
            // version our contract api models
            //store.Ignore(p => p.Email);
        }

        // see https://github.com/microsoft/aspnet-api-versioning/tree/master/samples/aspnetcore/SwaggerODataSample
        // for more examples
    }
}

Model Class:

public class Service
{
    public int Id { get; set; }
    public string name { get; set; }
}

Odata Installer - Api Explorer options:

options.QueryOptions.Controller<V1.ServicesController>()
                                    .Action(f => f.Get(default))
                                        .Allow(Skip | Count)
                                        .AllowTop(100)
                                        .AllowOrderBy("name");

ServiceController:

 /// <summary>
    /// Gets all services
    /// </summary>
    /// <param name="options">The current OData query options.</param>
    /// <returns>All available stores.</returns>
    /// <response code="200">The successfully retrieved stores.</response>
    [Produces("application/json")]
    [ProducesResponseType(typeof(Service), Status200OK)]
    //[Cached(600)]
    public async Task<IActionResult> Get(ODataQueryOptions<Models.Service> options)
    {
        _logger.LogInformation($"Hit: Stores: Get()");
        
        return Ok(new Service { Id = 123, name = "qqq" });
    }

When we run this code swagger opens up but does not recognize the GET method nor works in postman: 在此处输入图像描述

Expected:

GET /api/v1/services

Issue was enity set name should point to controller name. Below line fixed it: var services = builder.EntitySet("Services").EntityType;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM