如何解决Asp.Net Core odata无法识别控制器中的Get操作方法
我们在Asp.Net Core 3.1上使用了Odata 7.4.1,当我们使用Get操作方法添加新控制器时,它将无法正常工作。
Odata路由构建器:
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());
});
模型配置:
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
}
}
模型类别:
public class Service
{
public int Id { get; set; }
public string name { get; set; }
}
Odata安装程序-Api Explorer选项:
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" });
}
当我们运行此代码时,会突然打开,但无法识别GET方法,也无法在邮递员中使用:
预期:
获取/ api / v1 /服务
解决方法
问题为实体集名称应指向控制器名称。 在下面的行中修复它: var services = builder.EntitySet(“ Services”)。EntityType;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。