我有 2 个 POCO、教训和具有 int PK 的特征。
我设置了导航属性,以便我可以成功$expand
像这样:
http://localhost:54321/odata/Lessons?$expand=Traits
http://localhost:54321/odata/Traits?$expand=Lessons
我将项目从 Net 461 迁移到 .Net Core 2 的最后一个障碍是建立关系 https://learn.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/entity-relations-in-odata-v4#creating-a-relationship-between-entities.
具体来说,当我尝试使用以下请求调用以下方法时,我得到 404。
[AcceptVerbs("POST", "PUT")]
public async Task<IActionResult> CreateRef(
[FromODataUri] int key, string navigationProperty, [FromBody] Uri link)
{
.... Do Work
}
邮递员请求:
http://localhost:54321/odata/Lessons(1)/Traits/$ref
body:
{
"@odata.id":"http://localhost:54321/OData/traits(1)"
}
以下是我的Startup.Configure
method.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
...
var builder = ConfigureOdataBuilder(app);
app.UseMvc(routeBuilder =>
{
routeBuilder.Select().Expand().Filter().OrderBy().MaxTop(null).Count();
routeBuilder.MapODataServiceRoute("ODataRoute", "odata", builder.GetEdmModel());
// Work-around for #1175
routeBuilder.EnableDependencyInjection();
routeBuilder.MapRoute(name: "default", template: "{controller=Home}/{action=Index}/{id?}"); // enable mvc controllers
});
}
private ODataConventionModelBuilder ConfigureOdataBuilder(IApplicationBuilder app)
{
var builder = new ODataConventionModelBuilder(app.ApplicationServices);
builder.EntitySet<Lessons>(nameof(Lessons));
builder.EntitySet<Traits>(nameof(Traits));
return builder;
}
问题:我如何达到这个控制器方法?
我尝试过的事情,
- 将 CreateRef 重命名为 CreateLink 并创建
- 关注了这些 Git 问题中的每个链接,here https://github.com/OData/WebApi/issues/939 and
here https://github.com/OData/WebApi/issues/975#issuecomment-353488806.
- 继续阅读属性路由 http://odata.github.io/WebApi/#03-03-attrribute-routing spec.
- 尝试了基于此 DeleteRef 的解决方案Web Api oData v4 $ref 404 或服务器错误 https://stackoverflow.com/questions/36678752/web-api-odata-v4-ref-404-or-server-error
- 尝试明确定义路线
[ODataRoute("Lessons({key})/{navigationProperty}")]