简体   繁体   中英

C# Web API Always getting a null value on a POST

Screenshot of Postman

I've seen this question asked a few times but with no answer which seems to help or really fit my case. Here's my code first:

So here's my code; the value is always null no matter what I do, and by adding the [System.Web.Mvc.HttpPost] decoration I now get an error stating that the route doesn't support posts. I need some help.

[System.Web.Mvc.HttpPost]
public string Post(string value)
{
    return value;
}

Here is what I'm posting via Postman; I'm putting this in the body:

"test"

No matter what I put in the body, the value of "value" is always null. I'm not sure what to do here, nothing seems to work. I even changed the decoration to just [HttpPost]. Gets work just fine for this controller, it's just the post which is failing miserably.

Try

public ActionResult PostName([FromBody] string value)
 {
  ...
 }

And put "value" in Body type raw in PostMan.

{
    "value" : "hi"
}

Okay, so I created a simple Web API project using Visual Studio, so it has all the default routes and whatnot.

在此处输入图片说明

Then I grabbed postman and fiddled with it, until I got it to post properly to the endpoint. Note the format header of application/json, but since we're only sending a string, it's JUST a string.

在此处输入图片说明

Now the value is here in the breakpoint

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM