1

In my VS2013 Web Application I have many controllers like this:

[Route("Test/{param1}")]
public bool Test (Int32 Param1)
{   
  return true;  
}

I invoke the method from my client:

response = await client.GetAsync("TestCtrlClass/Test/1");

and all works nice.

Now, I need to pass an object to methods, so I put this:

[HttpPost]
[Route("Test2/{item}")]
public bool Test2([FromBody] ClassName item)
{
     return true;       
}

I invoke the method from my client:

HttpClient client = new HttpClient();
client.BaseAddress = ServerUri;
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

MediaTypeFormatter jsonFormatter = new JsonMediaTypeFormatter();
HttpContent content = new ObjectContent<ClassName>(Item, jsonFormatter);
response = await client.PostAsync("TestCtrlClass/Test2", content);

and i get 404 NOT FOUND.

this is my route configuration:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();

Why?

Thanks.

1 Answer 1

3

Note that you are sending a POST request with parameters in the body. That means that the URl you are posting to is TestCtrlClass/Test2, not TestCtrlClass/Test2/anything_here. So your attribute should be:

[HttpPost]
[Route("Test2")]
public bool Test2([FromBody] ClassName item)

Also I believe [FromBody] is not needed here.

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.