13

I cannot for the life of me figure this out. I have a web api controller with Get and Post methods. The Get method works fine with and without parameters, but the post breaks when I try to add a String parameter to it. Below is my code.

Route:

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "{controller}/{id}",
            defaults: new { id = UrlParameter.Optional }
        );

Controller:

public class AuditController : ApiController
{
    public String Post(String test)
    {
        return "Success : " + test;
    }

    public String Get(String test)
    {
        return "Success : " + test;
    }
}

Request:

    var request = WebRequest.Create("http://localhost:42652/Audit");
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        using (var writer = new StreamWriter(request.GetRequestStream()))
        {
            writer.Write("test=TEST");
        }
        WebResponse webResponse = request.GetResponse();

I've tried many variations on the request, I feel like there's something simple I'm missing. Thanks for your help.

2
  • 1
    Your route says it has a parameter named id, but in your method you pass a parameter named test. What shall it be? Also primitive type parameters are always tried to read from the Uri, so your post method won't work. I really suggest you to work through all the very nice ASP.Net Web API tutorials on the asp.net homepage! Commented Jun 11, 2013 at 15:44
  • Yes it is. Your Get-method represents the Route without the {id} parameter - because your id parameter is not present in the method. Your test-parameter is one of the optional query parameters, when you call Audit?test=value, but not on Audit/value. The answer you accepted sum this up good, but I still encourage you to really work through the tutorials! Commented Jun 12, 2013 at 6:48

2 Answers 2

22

Since you are expecting parameter test to come from body of a request, you would need to decorate it with FromBody attribute. Example: ([FromBody]String test). This is not true for other complex types, for example: Employee class which is implicitly considered to be coming from Body.

Rearding GET request. It should be working only with test coming from query string /Audit?test=Mike

Following blog post has more details about parameter binding: http://blogs.msdn.com/b/jmstall/archive/2012/04/16/how-webapi-does-parameter-binding.aspx

Also, I see that you are using WebRequest. Have you considered using HttpClient from System.Net.Http instead?

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

3 Comments

I added the FromBody attribute, and it will reach the service now, but the 'test' variable inside the post method is null unfortunately. I will have a look at HttpClient as well.
try sending just =TEST in your body.
For fuffuf fuffsake. WHY???? Thank you for this, it brought an end to an otherwise annoying morning. Mostly filled with totally unintuitive programming decisions.
3

Change your AuditController to include the FromBody attribute :

public class AuditController : ApiController
{
    public String Post([FromBody]String test)
    {
        return "Success : " + test;
    }

    public String Get(String test)
    {
        return "Success : " + test;
    }
}

cf. http://msdn.microsoft.com/en-us/library/system.web.http.frombodyattribute(v=vs.108).aspx

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.