Link to home
Start Free TrialLog in
Avatar of mark_norgate
mark_norgate

asked on

ASP.NET Web API 2 with Angular woes

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:

User generated image
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
Avatar of kaufmed
kaufmed
Flag of United States of America image

Try changing your controller method to:

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

Open in new window

Avatar of mark_norgate
mark_norgate

ASKER

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

Mark
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok thanks, I'll give that a shot when I get back to work on Monday.

M
Yep, that fixed it. Thank you very much!

Mark