简体   繁体   English

在 .net 核心 3.1 web Z8A5DA52ED1264417D35AZE 应用程序中将 model 发送到 HttpPost 方法时出现问题

[英]Problem sending model to HttpPost method in a .net core 3.1 web api application

I'm trying to hit my HttpPost method in a .net core 3.1 application.我正在尝试在 .net 核心 3.1 应用程序中使用我的 HttpPost 方法。 When i try to hit it, the model object is created but none of the properties are set.当我尝试点击它时,会创建 model object 但没有设置任何属性。 The behaviour is same when i try to invoke using Swagger as well as postman.当我尝试使用 Swagger 和 postman 调用时,行为是相同的。 I tried to add a model as simple as:我尝试添加一个 model,如下所示:

public class Student{
public string Name{get; set;}
}

[HttpPost]
[Route("TestMethod")]
public void TestMethod(Student stu) {}

public void TestMethod(string name) {}

The strange behaviour is, if i pass it as query string, it works, but somehow it doesn't work with json (using postman).奇怪的行为是,如果我将它作为查询字符串传递,它可以工作,但不知何故它不适用于 json(使用邮递员)。 Also, if i create a demo app, it works perfectly fine, but not with my existing application此外,如果我创建一个演示应用程序,它可以正常工作,但不适用于我现有的应用程序

i used FromBody attribute as well.我也使用了 FromBody 属性。

Can someone help me with this?有人可以帮我弄这个吗?

you have to use this action你必须使用这个动作

[HttpPost]
[Route("TestMethod")]
public IActionResult TestMethod([FromBody]Student stu) { return Ok(stu.Name)}

For a Post acton using Postman you have to select Body, raw, JSON in Postman menus.对于使用 Postman 的 Post 操作,您必须 select 主体,原始,JSON 在 Z6F061E1CAD11C1D7A287401D148A23BBD7A2F8Z 菜单中。 Your json should be你的 json 应该是

{ "Name":"Doe" }

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

相关问题 带有 [HTTPPost] 的 .Net Core 3.1 API Action 方法返回 405 并带有错误的响应头允许 - .Net Core 3.1 API Action method with [HTTPPost] returns 405 with incorrect Response Header Allow .net core 3.1 mvc razorpage 编辑方法 httppost 不起作用? - .net core 3.1 mvc razorpage edit method httppost not working? 如何将 .NET Core 3.1 API web 应用程序作为 Z0F4137ED1502B5045426083AA258 服务托管? - How to host a .NET Core 3.1 API web application as windows service? How to add .net core Web API project to an existing .NET core (3.1) Web Application project? - How to add .net core Web API project to an existing .NET core (3.1) Web Application project? Httppost 和 httpput 被 .net 核心 3.1 中的 CORS 阻止 - Httppost and httpput blocked by CORS in .net core 3.1 ASP.NET Web API (.NET Core 3.1) 中的端点路由问题 - Problem with endpoint routing in ASP.NET Web API (.NET Core 3.1) 带有charset1252的ASP.NET Core Web API HttpPost - ASP.NET Core Web API HttpPost with charset1252 .NET Core 3.1 API 方法中的 GZip 响应 - GZip response in API method with .NET Core 3.1 带有xml输入的.net core 2.0 web api httppost为null - .net core 2.0 web api httppost with xml input comes in as null ASP.NET Core Webb API 中的多个 HttpPost 方法 - Multiple HttpPost Method in ASP.NET Core Webb API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM