简体   繁体   English

WebApi NET Core 未收到请求 json,而在 NET 标准中却收到了

[英]WebApi NET Core not receiving request json, whereas it does in NET standard

I created a NET Core starter WebApi project and added a very simple method/object.我创建了一个 NET Core 入门 WebApi 项目并添加了一个非常简单的方法/对象。 Testing the endpoint with Fiddler the request body doesnt bind to the post param.使用 Fiddler 测试端点,请求正文不会绑定到 post 参数。 I had spent 2 hours of searching for a solution to no avail.我花了 2 个小时寻找解决方案无济于事。 For brevity I included my object in the controller.为简洁起见,我将 object 包含在 controller 中。

namespace CoreWebApi.Controllers
{
    using Microsoft.AspNetCore.Mvc;

    [Route("[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        [HttpPost]
        public Message Post([FromBody]Message message)
        {
            return message;
        }
    }

    public class Message
    {
        public string Application;

        public string StatusOverride;
    }
}

提琴手

网络核心

After a lot of head scratching, I created a NET Standard web api starter project, copy/paste of my method and object and worked first time.经过一番摸索,我创建了一个 NET Standard web api 入门项目,复制/粘贴我的方法和 object 并第一次工作。 Wondering if someone can enlighten me as to what I would need to do on my NET core webapi to get it working.想知道是否有人可以告诉我我需要在我的 NET 核心 webapi 上做什么才能使其正常工作。

using System.Web.Http;

namespace WebApplication9.Controllers
{
    public class ValuesController : ApiController
    {
        public Message Post([FromBody]Message message)
        {
            return message;
        }
    }

    public class Message
    {
        public string Application;

        public string StatusOverride;
    }
}

网络标准

Even though class members are public, they still need to have get/set accessors:即使 class 成员是公共的,他们仍然需要有 get/set 访问器:

public class Message
{
    public string Application { get; set; }
    public string StatusOverride { get; set; }
}

...and it works! ......它的工作原理!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WebApi与.NET核心和json验证 - WebApi with .NET core and json validation 如何在.net核心WebApi上获取请求字符串 - How to get request string on .net core WebApi .NET Core 3.1 WebApi - 来自另一个 WebApi 的直通 JSON 响应 - .NET Core 3.1 WebApi - Passthrough JSON response from another WebApi .Net Core API 未在 Post 请求中接收对象 - .Net Core API not receiving object in Post request ASP.NET Core 托管的 webapi 在 GET 请求上显示“SyntaxError: JSON.parse” - ASP.NET Core hosted webapi shows "SyntaxError: JSON.parse" on GET request .Net Core WebAPI:使用不同的 json 序列化程序选项来反序列化同一 controller 中的请求正文 - .Net Core WebAPI : use different json serializer option to deserialize request body in the same controller Swashbuckle + ASP.Net Core WebApi:Swagger 文档不包含用于版本选择的 Request-Header 或 QueryParameter? - Swashbuckle + ASP.Net Core WebApi: Swagger Document does not include Request-Header or QueryParameter for version selection? 模型绑定不适用于 ASP.NET Core 2 WebAPI 中的 POST 请求 - Model binding is not working on POST request in ASP.NET Core 2 WebAPI 如何在 .NET Core WebAPI 中自动记录每个请求? - How to auto log every request in .NET Core WebAPI? 在.net核心WebAPI项目中,哪些事件导致http请求被取消? - In a .net core WebAPI project, which events cause the http request to be cancelled?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM