asp.net-core - 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");
服务控制器:
/// <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 方法,也无法在邮递员中工作:
预期的:
获取 /api/v1/服务
解决方案
问题是实体集名称应指向控制器名称。下面的行修复了它: var services = builder.EntitySet("Services").EntityType;
推荐阅读
- angular - 错误:[HPM] 上下文无效。期待类似:“/api”或[“/api”,“/ajax”]
- javascript - 如何动态结束加载动画
- jax-rs - /voterRestService 上已经有一个端点正在运行
- c - printw() 和 mvwprintw() 不打印
- reactjs - 如何在样式组件中使用联合选择器
- python - 如何使用 django 管理面板在特定地址上传文件?
- xml - 使用 xslt 1.0 添加当前日期和时间
- java - 带有 Spring 更高版本的 HashMaps 的 LinkedCaseInsensitive 映射转换问题
- typescript - 使用冒号和 as 声明类型的语法有什么区别?
- php - 使用 php 和 singleton 只运行一次该过程