I am getting 405(Method not found) error in PUT and DELETE requests in my angular and WebAPI. GET and POST are working fine. I have checked all the solutions with this type of error on 'SO' but it didn't worked. I have added required handlers(with PUT/DELETE verbs) in my WebConfig, updated applicationhost.config of IIS EXpress and also uninstalled WebDAV module but still the problem persists.
Here is my controller code:
[RoutePrefix("api/BlogPost")]
public class BlogPostController : ApiController
{
// GET: api/BlogPost
public IQueryable<BlogPostModel> GetblogPostTb()
{
return db.blogPostTb;
}
// GET: api/BlogPost/5
[ResponseType(typeof(BlogPostModel))]
public IHttpActionResult GetBlogPostModel(int id)
{
BlogPostModel blogPostModel = db.blogPostTb.Find(id);
if (blogPostModel == null)
{
return NotFound();
}
return Ok(blogPostModel);
}
// PUT: api/BlogPost/5
[ResponseType(typeof(void))]
public IHttpActionResult PutBlogPostModel(int id, BlogPostModel blogPostModel)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != blogPostModel.ID)
{
return BadRequest();
}
db.Entry(blogPostModel).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!BlogPostModelExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
[ResponseType(typeof(BlogPostModel))]
public IHttpActionResult DeleteBlogPostModel(int id)
{
BlogPostModel blogPostModel = db.blogPostTb.Find(id);
if (blogPostModel == null)
{
return NotFound();
}
db.blogPostTb.Remove(blogPostModel);
db.SaveChanges();
return Ok(blogPostModel);
}
}
And here is the client side code:
var updateBlogPost = function (id, blogPost) {
return $http.put(blogPostsUrl+"/"+id, blogPost)
.then(function (response) {
return response;
})
Just for the info,I am working with WebAPI2, IIS Express 10 in Visual Studio Community 2015. I am not sure if this is the error of IIS EXpress 10 or Community version of VS.
This seems to be the known issue with attribute routing with WebAPI. Here update AcceptVerbs and Route attribute with DELETE and PUT methods like this :
[ResponseType(typeof(void))]
[Route("{id:int}")]
[AcceptVerbs("PUT")]
public IHttpActionResult PutBlogPostModel(int id, BlogPostModel blogPostModel)
{
// Your code
}
And Delete as :
[ResponseType(typeof(BlogPostModel))]
[Route("{id:int}")]
[AcceptVerbs("DELETE")]
public IHttpActionResult DeleteBlogPostModel(int id)
{
// Your Code
}
And also use AcceptVerbs attribute for GET method as well because these three(GET,PUT,DELETE) have same URL structure to call their methods.