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)
{
}
CrazyDog
  • 321
  • 3
  • 17

1 Answers1

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.

DarkUrse
  • 2,084
  • 3
  • 25
  • 33
  • Yup, that worked. Thanks a lot! I had a class just like that. I was used to stringify-it, therefor the string argument. – CrazyDog Feb 11 '15 at 09:26