0
votes

I have a controller with a method that returns some response based on the value of a parameter. I am trying to POST Json data to this controller but somehow the binding is not working. I am using Fiddler to test my controller method:

[AcceptVerbs(HttpVerbs.Post)]
public string Authenticate(string username)
{
    //some logic
    return "value";
}

now userName always returns null when I debug the application. To test this method I am using Fiddler. Raw data of the request is :

POST http://localsite/Home/authenticate HTTP/1.1
User-Agent: Fiddler
Host: localhost:52774
x-requested-with: XMLHttpRequest
Content-Length: 20
Content-Type: application/json; charset=utf-8
Accept: application/json

{"username":"kunal"}

Any guesses where I am going wrong in this.

1

1 Answers

0
votes

In MVC2 there is no model binder support for JSON out of the box.

If you don't plan to upgrade to MVC3/4 then you can find a good article: Sending JSON to an ASP.NET MVC Action Method Argument how to enable it in an MVC2 app.

Basically you need to create and add a JsonValueProviderFactory to the ValueProviderFactories...

Luckily you can find a implementation in ASP.NET MVC 2 Futures library