0

I want to make a PUT request to a web api server, using the following.

Angular resource and request:

Books: $resource('/api/book/:id',
                {id: '@id'},
                {
                    'update' : {method: 'PUT'}
                })
...

$id = book.BookId
BookLibraryAPI.Books.update({id: $id},book);

Web API controller:

public void Put(int id, string book)
{

}

But I get 405 error, with the following headers:

Request URL:http://localhost:53889/api/book/1009
Request Method:PUT
Status Code:405 Method Not Allowed
...

I tried many things, and I still cannot find the issue.

And the entire controller:

[Authorize] public class BookController : ApiController { BusinessBundle _bundle = new BusinessBundle();

// GET api/book
public IEnumerable<Book> Get()
{
    return _bundle.BookLogic.GetAll();
}

// GET api/book/5
public Book Get(int id)
{
    return _bundle.BookLogic.GetBook(id);
}

// POST api/book
public void Post([FromBody]string value)
{

}

// PUT api/book/5
public void Put(int id, string book)
{
    var a = book; // just for testing
}

// DELETE api/values/5
public void Delete(int id)
{
}
3
  • please add the controller(all the cs) Commented Feb 11, 2015 at 8:57
  • Maybe because the Put expects two parameters, id & book and you're only sending one ? Commented Feb 11, 2015 at 9:07
  • Try this stackoverflow.com/questions/19162825/… i think it may be the same issue Commented Feb 11, 2015 at 9:15

1 Answer 1

1

Something strikes me as strange, in .js you're doing the following :

$id = book.BookId
BookLibraryAPI.Books.update({id: $id},book);

Which means that book is a 'complex' object, but on the cs controller part you're receiving book as a string :

// PUT api/book/5
public void Put(int id, string book)
{
    var a = book; // just for testing
}

To start with, book cannot be a string since you're sending a full object. At worst it can be a generic object type, at best it should be a class with exactly the same members that you sent from the angular part.

So my suggestion is to make a class to match the book parameter you sent from the javascript.

ps: Put() on the asp.net controller does expect a second contrary to what was suggested in the above comments.

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

1 Comment

Yup, that worked. Thanks a lot! I had a class just like that. I was used to stringify-it, therefor the string argument.

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.