0
votes

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)
{
}
1
please add the controller(all the cs) - knightsb
Maybe because the Put expects two parameters, id & book and you're only sending one ? - Omri Aharon
Try this stackoverflow.com/questions/19162825/… i think it may be the same issue - knightsb

1 Answers

1
votes

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.