1

I am trying to pass a parameter of an object through a post request in a web api microsoft application, but the parameter passed is always null.

This is my Controller for post method, under debugging the program reached the method but dataUrlIN.url string is always empty

[HttpPost]
public void Post(DataUrl dataUrlIN)
{
    string a = dataUrlIN.url;
    a += a + "/test";
}

This is the object of DataUrl

public class DataUrl
{
    public string url { get; set; }
}

This is the the post request

POST /api/values HTTP/1.1
Host: localhost:50673
Content-Type: application/json
User-Agent: PostmanRuntime/7.11.0
Accept: */*
Cache-Control: no-cache
Postman-Token: a12b47ba-8595-4297-b3e1-380052fb2a46,b4b4d699-206b-47ff-9fa3-71d06d4334d2
Host: localhost:50673
accept-encoding: gzip, deflate
content-length: 28
Connection: keep-alive
cache-control: no-cache

{
    "url" : "helloWorld"
}

After sending the post request the breakpoint in the method public void Post(Data Url dataUrlIN) is triggered but the url string is empty "HelloWorld" string never appears

1
  • Add [FromBody] to the parameter Commented May 13, 2019 at 19:24

1 Answer 1

1

Add the FromBody attribute to the parameter

[HttpPost]
public void Post([FromBody]DataUrl dataUrlIN)
{
}

Check out the Parameter Binding Documentation for more information

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.