首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >控制器中的Asp.Net核数据不识别Get动作方法

控制器中的Asp.Net核数据不识别Get动作方法
EN

Stack Overflow用户
提问于 2020-08-13 10:51:11
回答 1查看 753关注 0票数 0

我们在Asp.Net Core3.1上使用OData7.4.1,当我们添加带有Get操作方法的新控制器时,它不起作用。

Odata路由生成器:

代码语言:javascript
复制
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());
            });

模型配置:

代码语言:javascript
复制
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
    }
}

模型类:

代码语言:javascript
复制
public class Service
{
    public int Id { get; set; }
    public string name { get; set; }
}

Odata安装程序- Api选项:

代码语言:javascript
复制
options.QueryOptions.Controller<V1.ServicesController>()
                                    .Action(f => f.Get(default))
                                        .Allow(Skip | Count)
                                        .AllowTop(100)
                                        .AllowOrderBy("name");

ServiceController:

代码语言:javascript
复制
 /// <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" });
    }

当我们运行此代码时,swagger会打开,但不识别GET方法,也不会在postman中工作:

预期:

GET /api/v1/services

EN

回答 1

Stack Overflow用户

发布于 2020-08-13 16:17:43

问题是敌意设置名称应该指向控制器名。下面一行固定了它: var services = builder.EntitySet("Services").EntityType;

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63393536

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档