asp.net-core - ASP.NET Core Odata 服务发布不工作
问题描述
我在我的 ASP.NET Core 应用程序中实现了 ODATA 服务。GET 函数工作正常,但 POST 函数有一些问题。
如果我执行 POST 程序正在执行正确的方法,但我没有收到任何数据。我的代码中是否缺少任何内容?
控制器:
[EnableCors]
[ODataRoutePrefix("documents")]
public class DocumentController : ODataController
{
[ODataRoute]
[EnableQuery]
public Document PushDocument([FromBody]Document doc)
{
System.Diagnostics.Debug.WriteLine("DomentID: " + doc.Id);
System.Diagnostics.Debug.WriteLine("Dokument: " + doc.RawDocument);
return doc;
}
}
解决方案
既然使用[FromBody]
了,就需要Content-Type: application/json
在postman中发送数据为:
启动.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddOData();
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.UseHttpsRedirection();
app.UseMvc(b =>
{
b.MapODataServiceRoute("odata", "odata", GetEdmModel());
});
}
private static IEdmModel GetEdmModel()
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Document>("Documents");
builder.EntitySet<Press>("Presses");
return builder.GetEdmModel();
}
推荐阅读
- react-native - Victory Native:图表未显示 Y 轴的第一个标签
- powershell - 仅从返回的 Get-ADUser 数组中获取字符串值
- reactjs - 反应中的嵌套地图返回一个空页面
- python - 清除 Tkinter GUI 中每个选项卡的画布
- asp.net-core - 使用 DevExtreme 在 ASP.NET 中发布时如何获取自动完成文本框值
- java - java 8流groupingBy进入自定义对象的集合
- android - JobIntentServices 是否限制线程数?具有多个 JobIntentServices 的应用程序只为所有服务提供两个线程
- machine-learning - 'OneHotEncoder' 对象没有属性 'transform'
- node.js - 错误:在任何 orm 配置文件中都找不到连接选项
- crash - Nuget PM VS 2019 崩溃