We help IT Professionals succeed at work.

ASP.NET Web API 2 with Angular woes

mark_norgate
mark_norgate asked
on
Hi there

This question relates to a problem I'm having with my (first) Web API 2 project. One of the actions on my controller is being hit successfully, but the values of the parameter passed in are all in their default states and do not correspond to the values sent from the Angular behaviour.

My controller looks like this:

[HttpPost]
public IEnumerable<string> Post(SearchParameters id)
{
    return null;
}

public struct SearchParameters
{
    string brokerIsUnallocated;
    string brokerIncludeDeleted;
    string businessType;
    bool codeC;
    bool codeD;
    bool codeP;
    bool codeS;
    bool codeT;
    bool codeX;
    string companyName;
    string contactName;
    string country;
    string customerId;
    string department;
    string selectedBroker;
    string town;
}

Open in new window


My Web API 2 controller is successfully being hit, but the view model passed as a parameter is not being populated. It contains nulls for strings and falses for booleans.

My AngularJS call looks like this:

        $scope.search = function () {
            var id = { id : {
                        selectedBroker: typeof ($scope.selectedBroker) === 'undefined' ? 0 : $scope.selectedBroker.userIdField,
                        brokerIsUnallocated: $scope.brokerIsUnallocated || false,
                        brokerIncludeDeleted: $scope.brokerIncludeDeleted || false,
                        customerId: $scope.customerCustomerId || 0,
                        businessType: $scope.customerBusinessType || "",
                        companyName: $scope.customerCompanyName || "",
                        town: $scope.customerTown || "",
                        department: $scope.selectedDepartment || 0,
                        contactName: $scope.customerContactName || "",
                        country: $scope.selectedCountry || 0,
                        codeP: $scope.codeP || false,
                        codeC: $scope.codeC || false,
                        codeT: $scope.codeT || false,
                        codeS: $scope.codeS || false,
                        codeX: $scope.codeX || false,
                        codeD: $scope.codeD || false
                    }
                }

            $http({
                method: 'POST',
                url: '/api/customer',
                data: id
            });
        }

Open in new window


Fiddler looks like this:

Fiddler capture
I am using the default routing, like this:

public static void Register(HttpConfiguration config)
{
    config.MapHttpAttributeRoutes();

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

Open in new window


Can anyone suggest what's going on?

Looking forward to your responses. Mark
Comment
Watch Question

Most Valuable Expert 2011
Top Expert 2015

Commented:
Try changing your controller method to:

[HttpPost]
public IEnumerable<string> Post([FromBody]SearchParameters id)
...

Open in new window

Author

Commented:
I did try that today, but it had no effect.

Mark
Most Valuable Expert 2011
Top Expert 2015
Commented:
You also need to remove "id: { .. }" wrapper you have in the Javascript. Just use an anonymous object.

Author

Commented:
Ok thanks, I'll give that a shot when I get back to work on Monday.

M

Author

Commented:
Yep, that fixed it. Thank you very much!

Mark