ASP.NET Core Odata服务发布不起作用

时间:2019-11-12 14:43:16

标签: 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;
        }
}

1 个答案:

答案 0 :(得分:0)

由于您使用[FromBody],因此需要以Content-Type: application/json的身份在邮递员中发送数据: enter image description here

Startup.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();
    }