简体   繁体   English

在asp.net核心中调用asp.net mvc 5 Web api,FromBody始终为null

[英]calling asp.net mvc 5 web api in asp.net core, FromBody always null

When I call the asp.net mvc 5 web api in Asp.net Core the input parameter FromBody always is null 当我在Asp.net Core中调用asp.net mvc 5 Web api时,输入参数FromBody始终为null

asp.net Mvc 6 web api asp.net Mvc 6 Web API

public void Post([FromBody]string id)
{

}

calling web api in asp.net core 在asp.net核心中调用Web API

using (var client = new HttpClient())
  {
      string stringData = JsonConvert.
      SerializeObject("test");
      var contentData = new StringContent(stringData, System.Text.Encoding.UTF8,"application/json");
      HttpResponseMessage response1 = client.PostAsync("http://localhost:49741/api/values/", contentData).Result;      
  }

As Crowcoder said. 正如Crowcoder所说。 You should register MediaTypeFormatter . 您应该注册MediaTypeFormatter Here simple arctile how do that provides to do what you want. 这里简单arctile 怎么做 ,它提供给你想要做什么。

But simpler is choose another way to work with incoming data. 但更简单的是选择另一种处理传入数据的方法。 As application/json . 作为application/json Create simple Dto class like: 创建简单的Dto类,例如:

public class TestDataDto
{
    public string Id { get; set; }
}

And change argument type of your Post method: 并更改Post方法的参数类型:

// POST api/values
public void Post([FromBody]TestDataDto value)
{

}

Then code of your core client should be like this: 然后,核心客户的代码应如下所示:

static void Main(string[] args)
{
    using (HttpClient httpClient = new HttpClient())
    {
        var someData = JsonConvert.SerializeObject(new { Id = "test" });
        var content = new StringContent(someData, System.Text.Encoding.UTF8, "application/json");

        var result = httpClient.PostAsync("http://localhost:55878/api/values", content).Result;
        if (result.IsSuccessStatusCode)
        {
            Console.WriteLine("All is fine");
        }
        else
        {
            Console.WriteLine($"Something was wrong. HttpStatus: {result.StatusCode}");
        }
    }

    Console.ReadKey();
}

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

相关问题 Asp.net 核心 FromBody 总是得到 NULL - Asp.net core FromBody always getting NULL FromBody 在 ASP.NET 核心 API 返回 null - FromBody in ASP.NET Core API returns null 在 ASP.Net Core 5 MVC 控制器中,当传递包含小数的 JSON 对象 FromBody 时,模型始终为空 - In ASP.Net Core 5 MVC Controller, when passed a JSON object FromBody that contains a decimal the model is always null 消耗asp.net web api的android改造[FromBody]始终为null - android retrofit consuming asp.net web api [FromBody] always null ASP.NET 核心 MVC 调用 API - ASP.NET Core MVC calling an API 通过[FromBody]到MongoDB GeoJsonObjectModel成员的POST / PUT到ASP.Net Core始终为null - POST/PUT to ASP.Net Core with [FromBody] to a MongoDB GeoJsonObjectModel member is always null 与 ASP.NET Core 中的 FromBody 混淆 - Confused with FromBody in ASP.NET Core ASP.NET 核心 - [FromBody] 的奇怪行为 - ASP.NET Core - weird behavior with [FromBody] 发布到 ASP.NET Core 3.1 Web 应用程序时,“[FromBody]MyClass 数据”通常为空 - When posting to an ASP.NET Core 3.1 web app, "[FromBody]MyClass data" is often null 在ASP.NET Core MVC中无法调用具有类参数带有[FromBody]属性的类的API Post方法 - Unable to call API Post Method that has a class parameter with [FromBody] attribute in asp.net core mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM