Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net 4 Web API receiving NULL data in parameters

I am a Web API newbie writing a RESTFul Web Api service in .Net 4. I have configured a controller for a class I am calling Authentication. The problem I am having is that when I Post or Put JSON data to the web api service, my handlers in the controller are called, but the parameter objects have NULL data in them. I can successfully Get data, but Post and Put come up with empty parameter objects.

I have tested from a client application and Fiddler, and I get the same result. Here is what I am sending from Fiddler:

Action: PUT
URI: http://127.0.0.1:81/Authentication/xyz/
HTTP Version: HTTP/1.1
Request Headers:
User-Agent: Fiddler
Host: 127.0.0.1:81
Content-Type: application/json; charset=utf-8
Content-Length: 75
Request Body:
{"hashvalue":"kbVFNeBbNHmYQXXlS6d7Ag==","password":"test","username":"dan"}

When I set a breakpoint in the Put handler in the Authentication controller:

    public void Put(string IDstring, Authentication value)
    {

    }

I get the following in the watch window:

IDstring                    null    string
value   {bbsRESTResources.Authentication}   bbsRESTResources.Authentication
    hashvalue   null    string
    password    null    string
    username    null    string

my Authentication class looks like this:

namespace bbsRESTResources
{
    public class Authentication
    {
        public string username;
        public string password;
        public string hashvalue;
    }
}

my Route looks like this:

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

Please don't concern yourself with whether or not this is a good way to handle authentication. This is just a first pass at testing my application from end-to-end. I have looked for examples of other people having this problem and have found none.

like image 386
Dan Avatar asked May 08 '12 01:05

Dan


1 Answers

It turns out that the model binder only works on properties, not fields. As a result, I needed to make properties out of the individual members of the Authentication class. When I changed my definition of the Authentication class to the following, it works:

public class Authentication
{
    public string hashvalue {get; set;}
    public string password { get; set; }
    public string username { get; set; }
}
like image 174
Dan Avatar answered Oct 13 '22 04:10

Dan