Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
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! – JustAnotherUserYouMayKnow Jun 11 at 15:44
is this ^^ accurate? – Jonesy Jun 11 at 17:18
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! – JustAnotherUserYouMayKnow 2 days ago

2 Answers

up vote 1 down vote accepted

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?

share|improve this answer
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. – Jonesy Jun 11 at 17:10
try sending just =TEST in your body. – Kiran Challa Jun 11 at 17:13
fantastic that did it! Thankyou for your insight – Jonesy Jun 11 at 17:17

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.