Rest OData GetById未在Asp.net Core 2.2上运行
我试图从我的元素列表中获取一个项目,但它不起作用。我正确地使用了FromODataUri,我看不出有什么问题。 我可以点击以下资源: 但我一件也买不到: 这就是我的Startup.cs的样子Rest OData GetById未在Asp.net Core 2.2上运行,rest,asp.net-core,.net-core,odata,asp.net-core-webapi,Rest,Asp.net Core,.net Core,Odata,Asp.net Core Webapi,我试图从我的元素列表中获取一个项目,但它不起作用。我正确地使用了FromODataUri,我看不出有什么问题。 我可以点击以下资源: 但我一件也买不到: 这就是我的Startup.cs的样子 using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNet.OData.Builder; using Microsoft.Asp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.OData.Builder;
using Microsoft.AspNet.OData.Extensions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.OData.Edm;
using OdataTest.Models;
using Swashbuckle.AspNetCore.Swagger;
namespace OdataTest
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddOData();
services.AddODataQueryFilter();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "Inventory Api", Version = "v1" });
});
services.AddMvc(options => options.EnableEndpointRouting = false).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseSwagger();
app.UseSwaggerUI(c =>
{
// force to add another /swagger to fix issue
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Physical Inventory API");
});
app.UseHttpsRedirection();
app.UseMvc(b =>
b.MapODataServiceRoute("odata", "odata", GetEdmModel(app.ApplicationServices))
);
}
private static IEdmModel GetEdmModel(IServiceProvider serviceProvider)
{
ODataModelBuilder builder = new ODataConventionModelBuilder(serviceProvider);
builder.Namespace = "PartsInventory";
builder.ContainerName = "PartsInventoryContainer";
builder.EntitySet<InventoryOutputDto>("Inventories").EntityType
.Filter()
.Count()
.Expand()
.OrderBy()
.Page()
.Select();
return builder.GetEdmModel();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.AspNet.OData.Builder;
使用Microsoft.AspNet.OData.Extensions;
使用Microsoft.AspNetCore.Builder;
使用Microsoft.AspNetCore.Hosting;
使用Microsoft.AspNetCore.HttpsPolicy;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.Extensions.Configuration;
使用Microsoft.Extensions.DependencyInjection;
使用Microsoft.Extensions.Logging;
使用Microsoft.Extensions.Options;
使用Microsoft.OData.Edm;
使用OdataTest.模型;
使用swashback.AspNetCore.Swagger;
名称空间OdataTest
{
公营创业
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
services.AddOData();
services.AddODataQueryFilter();
services.AddSwaggerGen(c=>
{
c、 SwaggerDoc(“v1”,新信息{Title=“库存Api”,Version=“v1”});
});
services.AddMvc(options=>options.EnableEndpointRouting=false);
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
其他的
{
//默认的HSTS值为30天。您可能希望在生产场景中更改此值,请参阅https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseSwagger();
app.UseSwaggerUI(c=>
{
//强制添加另一个/招摇过市以解决问题
c、 SwaggerEndpoint(“/swagger/v1/swagger.json”,“物理库存API”);
});
app.UseHttpsRedirection();
app.UseMvc(b=>
b、 MapODataServiceRoute(“odata”、“odata”、GetedModel(app.ApplicationServices))
);
}
私有静态IEdmModel GetEdmModel(IServiceProvider服务提供程序)
{
ODataModelBuilder=新ODataConventionModelBuilder(服务提供商);
builder.Namespace=“PartsInventory”;
builder.ContainerName=“PartsInventoryContainer”;
builder.EntitySet(“库存”).EntityType
.Filter()
.Count()
.Expand()
.OrderBy()
.第页()
.Select();
返回builder.GetEdmModel();
}
}
}
还有我的控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.OData;
using Microsoft.AspNet.OData.Routing;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using OdataTest.Models;
namespace OdataTest.Controllers
{
public class PartsInventoryController : ODataController
{
List<InventoryOutputDto> list = new List<InventoryOutputDto>() {
new InventoryOutputDto {
Description = "Description1",
UserName = "user1",
EndDate = DateTime.Now,
ErrorId = "Error",
InventoryId = 1,
LocationCode = 1,
StartDate = DateTime.Now,
StatusCode = 1
},
new InventoryOutputDto {
Description = "Description1",
UserName = "user1",
EndDate = DateTime.Now,
ErrorId = "Error2",
InventoryId = 2,
LocationCode = 2,
StartDate = DateTime.Now,
StatusCode = 2
} };
[HttpGet]
[ODataRoute("Inventories")]
/// public IActionResult Inventory(InventoryInputDto inputDto)
public IActionResult GetInventories(InventoryInputDto inputDto)
{
return StatusCode(StatusCodes.Status200OK, list);
}
[HttpGet]
[ODataRoute("Inventories({key})")]
public IActionResult GetInventoryById([FromODataUri] int key)
{
var invRecord = list.FirstOrDefault(i => i.InventoryId == key);
if(invRecord == null)
{
return NotFound();
}
return Ok(invRecord);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.AspNet.OData;
使用Microsoft.AspNet.OData.Routing;
使用Microsoft.AspNetCore.Http;
使用Microsoft.AspNetCore.Mvc;
使用Newtonsoft.Json;
使用OdataTest.模型;
命名空间OdataTest.Controller
{
公共类PartsInventoryController:ODataController
{
列表=新列表(){
新的库存输出到{
Description=“Description1”,
UserName=“user1”,
EndDate=DateTime.Now,
ErrorId=“Error”,
InventoryId=1,
位置代码=1,
StartDate=日期时间。现在,
状态代码=1
},
新的库存输出到{
Description=“Description1”,
UserName=“user1”,
EndDate=DateTime.Now,
ErrorId=“Error2”,
InventoryId=2,
位置代码=2,
StartDate=日期时间。现在,
状态代码=2
} };
[HttpGet]
[ODataRoute(“库存”)]
///公共IActionResult清单(InventoryInputDto inputDto)
公共IActionResult GetInventory(InventoryInputDto inputDto)
{
返回状态码(StatusCodes.Status200OK,列表);
}
[HttpGet]
[ODataRoute(“目录({key})”)]
public IActionResult GetInventoryById([FromODataUri]int键)
{
var invRecord=list.FirstOrDefault(i=>i.InventoryId==key);
if(invRecord==null)
{
返回NotFound();
}
返回Ok(invRecord);
}
}
}
“OData GetById不工作”:您能告诉我们您遇到了什么错误吗?我复制并粘贴了你的代码,但对我来说效果很好。没有到达控制器。在输出上,我没有收到任何错误。我再次使用ASP.NET Core v2.2.402
+Microsoft.AspNetCore.OData v7.2.1
)对其进行测试。但对我来说效果很好。是否有复制的演示?