首页 > 解决方案 > 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;
        }
}

标签: asp.net-coreodata

解决方案


既然使用[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();
    }

推荐阅读