1

Hi every one I am new to Asp.net Web API and I had my question post data using asp.net Web API and got my answer accepted.

This is an extension to the same question I want to post data with some header value in the Postman and my code is as follows

public HttpResponseMessage PostCustomer([FromBody] NewUser userData, string devideId)
{
    //My code 
    return response;
}

When I hit this in Postman passing values in JSON format in BODY - raw I got message as follows

No HTTP resource was found that matches the request URI No action was found on the controller that matches the request.

Please help me.

1
  • Show your routing configuration and how exactly you send this request. Commented Apr 18, 2016 at 11:38

1 Answer 1

2

It looks like you have added some additional devideId string parameter to your action. Make sure that you are supplying a value to it as a query string when making the request:

POST http://localhost:58626/api/customers?devideId=foo_bar

If you don't want to make this parameter required then you should make it optional (in terms of optional method parameter in .NET):

public HttpResponseMessage PostCustomer([FromBody] NewUser userData, string devideId = null)
{
    ...
}

Now you can POST to http://localhost:58626/api/customers without providing a value for this parameter.

Remark: You don't need to decorate a complex object type (such as NewUser) with the [FromBody] attribute. That's the default behavior in Web API.


UPDATE: Here's how you could read a custom header:

public HttpResponseMessage PostCustomer(NewUser userData)
{
    IEnumerable<string> values;
    if (this.Request.Headers.TryGetValues("X-MyHeader", out values))
    {
        string headerValue = values.FirstOrDefault();
    }

    ...

    return response;
}
Sign up to request clarification or add additional context in comments.

2 Comments

instead of passing as parameter how can I read from header and get the header value?
By using the Request.Headers collection in your code and more specifically the TryGetValues method on it. I have updated my answer to illustrate it with an example.

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.