简体   繁体   English

为什么在 controller 中收不到 post 请求?

[英]Why I can't get a post request in the controller?

I want to get the body of a post in a controller.我想在 controller 中获取帖子的正文。

Here is my code:这是我的代码:

[HttpPost]
public string GetMsg([FromBody]GetMsgModel GSM)
{
    return "";
}

And posted class并贴出 class

public class GetMsgModel { 
    public string ToUserName { get; set; }
    public string FromUserName { get; set; }
    public string CreateTime { get; set; }
    public string MsgType { get; set; }
    public string Content { get; set; }
    public string MsgId { get; set; }
}

I added a breakpoint to the GetMsg and send a post via postman with this XML body:我在GetMsg中添加了一个断点,并通过 postman 使用此 XML 正文发送帖子:

<xml>
  <ToUserName>123</ToUserName>
  <FromUserName>456</FromUserName>
  <CreateTime>1348831860</CreateTime>
  <MsgType>789</MsgType>
  <Content>000</Content>
  <MsgId>1234567890123456</MsgId>
</xml>

在此处输入图像描述

Well, the breakpoint does not run at all.好吧,断点根本不运行。

What's wrong with this?这有什么问题? There is another HttpGet method in the same controller and it works well.在同一个 controller 中还有另一个HttpGet方法,它运行良好。 It seems it is not the problem of the controller yet.似乎还不是 controller 的问题。

If you're using Net Core Add to Statup.cs如果您使用的是 Net Core 添加到 Statup.cs

services.AddXmlSerializerFormatters();

And the XML should be XML 应该是

<GetMsgModel>
  <ToUserName>123</ToUserName>
  <FromUserName>456</FromUserName>
  <CreateTime>1348831860</CreateTime>
  <MsgType>789</MsgType>
  <Content>000</Content>
  <MsgId>1234567890123456</MsgId>
</GetMsgModel>

Hope it helps希望能帮助到你

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

相关问题 我无法从 AJAX 中的 C# controller 发布请求中获取数据 - I can't get data from AJAX post request in C# controller 为什么我无法通过HttpWebRequest获得请求? - Why I can't get request with HttpWebRequest? 无法从帖子请求答案中获取0 - can't get 0 from post request answer 为什么我无法访问通过 POST 请求以编程方式添加的 static 文件? - Why can't I access static files that I add programmatically from a POST request? 为什么我不能使用UnityWebRequest向Azur Face Vision发送帖子请求,但是它可以与HttpClient一起使用? - Why i can't send post request to Azur Face Vision with UnityWebRequest but's is work with HttpClient? Asp.net MVC 4 Ajax发布请求不起作用,我不知道为什么 - Asp.net MVC 4 Ajax post request not working and I can't figure out why ASP.Net Core 6,SPA,端点 - 我无法从 http 调用 controller 获取请求 - ASP.Net Core 6, SPA, endpoint - I can't call a controller from http get request 我可以使用 Ocelot 将 POST http 请求重新路由到 GET http 请求吗? - Can I Reroute POST http request to a GET http request with Ocelot? 我无法通过 Ajax 帖子调用 Controller 操作 - I can't call Controller Action via Ajax Post 如何在post方法中获得实际的控制器 - How can i get the actual controller in post method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM