1

So, first of all I best explain that I am calling this method via AngularJS. I have many other controllers, but what makes this one unique is that the model only has one property:

public class Subscriber
{
    public string Email { get; set; }
}

And so my controller method (which is very simple) just looks like this:

[HttpPost]
[Route("")]
public async Task<IHttpActionResult> Create(Subscriber model)
{

    // Try to find the subscriber
    var subscriber = await this.service.GetAsync(model.Email);

    // If the subscriber does not exist
    if (subscriber != null) {

        // Create the subscriber
        this.service.Create(model);

        // Save the datbase changes
        await this.unitOfWork.SaveChangesAsync();
    }

    return Ok();
}

I didn't create a RequestModel although I might just do that to stop my method being invoked when the model is null.

So, the issue is just that, the model is always null. I have my AngularJS service which looks like this:

.service('SubscriberService', ['Api', function (api) {
    var apiUrl = 'api/subscribers';

    // Subscribe
    this.subscribe = function (email) {

        // Create our model
        var model = {
            email: email
        };

        console.log(model);

        // Create our subscriber
        return $http.post(apiUrl, model);
    };
}])

in that, the console log shows the model with the email value. In my web config I have this line:

serializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

which handles the camel case, although I have also tried with Pascal case to no avail.

Does anyone know why it isn't working? I have spent too much time staring at it.

6
  • Did you inspect if the call to the action is actually made? The rout is correct? Commented Aug 14, 2015 at 15:42
  • Sholdn't url be 'api/subscribers/create'? Commented Aug 14, 2015 at 15:42
  • This code looks fine. It should work. Model binding does not care about casing.Both PascalCasing and camelCasing will work as long as the property names match. Commented Aug 14, 2015 at 15:44
  • yeah I used fiddler, I can see that the model is being posted. I put a breakpoint on the actual method and it hits it, just that there is no model.... Commented Aug 14, 2015 at 15:47
  • @vidalsasoon has the correct answer below. You need to serialize your js model to json and you need to add the [FromBody] attribute to your method signature. Commented Aug 14, 2015 at 17:26

4 Answers 4

2

In your second block of code you are using the following:

return Ok();

By doing so you are only answering your api call with an empty HTTP 200 response. You should be answering with the model loaded from the database.

I'd suggest trying:

return Ok(subscriber);

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

I want to return an empty 200 response, it's a subscriber method, so nothing should be returned apart from the status code.
1

try adding this: "[FromBody]"

public async Task<IHttpActionResult> Create([FromBody]Subscriber model)

Also worth trying is:

return $http.post(apiUrl, angular.toJson(model));

1 Comment

Hi, neither of these worked. I don't do this anywhere else in my project so I would have been surprised if it had of :o I am starting the think that I am doing something stupid somewhere but can't see it.
0

Have you tried stringifying your model so it can be serialized as json in your api method?

return $http.post(apiUrl, JSON.stringify(model));

Comments

0

I am fairly certain that this won't help anyone else, but the issue was with the controller itself. I had copied it from one of my other controllers because the methods in it were nearly the same and one thing that I copied along with it was the Authorize annotation. I am using code first EF and I had just deleted my database and recreated it, but the angularJS still had a token for me so figured I was authenticated and the new controller allowed me to get into it but didn't return a 401 error because nothing was getting passed to it.

Anyway, removing Authrozie from the controller fixed the issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.