C# 错误请求400从API修补程序方法返回

C# 错误请求400从API修补程序方法返回,c#,postman,httpclient,asp.net-core-webapi,http-patch,C#,Postman,Httpclient,Asp.net Core Webapi,Http Patch,我正在使用.net核心web api。在我的API控制器类中,有如下PATCH方法 [HttpPatch("updateMessageTemplate/{templateId}")] public IActionResult UpdateMessageTemplate([FromHeader] int tenantId, int templateId,[FromBody] testClass msg) { try { //Some implementation i

我正在使用.net核心web api。在我的API控制器类中,有如下
PATCH
方法

[HttpPatch("updateMessageTemplate/{templateId}")]
public IActionResult UpdateMessageTemplate([FromHeader] int tenantId, int templateId,[FromBody] testClass msg)
{
    try
    {
        //Some implementation is here
        return Accepted();
    }
    catch
    {
        return StatusCode(500);
    }
}
public class testClass
{
    public string body { get; set; }
}
testClass
如下所示

[HttpPatch("updateMessageTemplate/{templateId}")]
public IActionResult UpdateMessageTemplate([FromHeader] int tenantId, int templateId,[FromBody] testClass msg)
{
    try
    {
        //Some implementation is here
        return Accepted();
    }
    catch
    {
        return StatusCode(500);
    }
}
public class testClass
{
    public string body { get; set; }
}
我从postman调用了API,并返回了它的请求

我将断点放在控制器方法中,但它没有命中。在我从方法参数breakpoin hit中删除
[FromBody]testClass msg
后,没有返回
400
。当我使用
[FromBody]testClass msg
时,为什么它返回400?如何从HTTP客户端调用此控制器方法

我试过这个,它也会返回400个请求

string serviceUrl = string.Format("{0}/notification/updateMessageTemplate/{1}", ConfigurationManager.AppSettings["LtApiUrl"], templateID);

string json = "[{\"body\":\"sample text\"}]";

HttpClient client = new HttpClient();
HttpMethod method = new HttpMethod("PATCH");
HttpRequestMessage message = new HttpRequestMessage(method, serviceUrl);

StringContent content = new StringContent(json, Encoding.UTF8, "application/json");
client.DefaultRequestHeaders.Add("tenantId", tenantId.ToString());
client.DefaultRequestHeaders.Add("Authorization", string.Format("bearer {0}", token));
message.Content = content;

var response = client.SendAsync(message).Result;               
return response.StatusCode.ToString();
我怎样才能解决这个问题?请帮帮我。我删除了前面的问题,这才是我真正的问题

更新:


我把邮递员的要求改为


之后,它的工作。但当我通过http客户机代码调用它时,它提供了
400 BADDREQUEST
。如何通过http客户端以正确的方式提供JSON正文,请尝试一下

[HttpPatch("updateMessageTemplate/{templateId}")]
public IActionResult UpdateMessageTemplate([FromHeader] int tenantId, int templateId, 
[FromBody] JsonPatchDocument<testMsg> msg)
 {
 try
 {
    //Some implementation is here
    return Accepted();
 }
 catch
 {
    return StatusCode(500);
 }
}
[HttpPatch(“updateMessageTemplate/{templateId}”)]
public IActionResult UpdateMessageTemplate([FromHeader]int-tenantId,int-templateId,
[FromBody]JsonPatchDocument msg)
{
尝试
{
//这里有一些实现
返回已接受();
}
抓住
{
返回状态码(500);
}
}

对于使用
FromBody
,您需要使用json而不是表单数据发送请求。您可以如下所示更改邮递员:

string json = "{\"body\":\"sample text\"}";
1.将ContentType更改为
application/json

2.将正文更改为raw并选择JSON样式:

更新:

您需要更改json,如下所示:

string json = "{\"body\":\"sample text\"}";

请不要提出新问题。编辑您原来的问题。@canton7我删除了它,先生,没有人关心这个问题:(我真的被这个问题困扰了。请帮助meI仍然建议增加服务器上的日志记录,并让它告诉您请求有什么问题。同时尝试删除位。去掉标题。这样做有效吗?删除url参数、正文,使其成为get或post,而不是补丁。找出导致问题的具体位lem@canton7我将邮递员请求更改为。之后它就可以工作了。但是当我通过http客户端代码调用它时,它提供了400个请求。如何传递[FromBody]以正确的方式不起作用。有没有合适的方法通过调试找到真正的问题我将邮递员请求更改为。在那之后,它工作了。但是当我通过http客户端代码调用它时,它提供了400个请求。是的,我这样做了,并通过邮递员解决了它。但是我如何通过http客户端代码调用它(请检查我的有问题的代码).请帮我解决这个问题。