Reputation: 1293
I have read various posts around the message
The requested resource does not support http method 'POST'
I have made sure I have included the Http
attribute. I have tried both System.Web.HttpPost
and System.Web.Mvc.HttpPost
I have also included the [FromBody]
tag
I have an angularjs page trying to post to a MVC method
Here is my angularjs call
$http({
url: '/api/entry',
method: 'POST',
headers: {
'Content-Type': 'application/json', /*or whatever type is relevant */
'Accept': 'application/json' /* ditto */
},
data: {
'data': $scope.userEntry
}
}).success(function (data, status, headers, config) {
$scope.Message = "Saved successfully";
$scope.working = false;
}).error(function (data, status, headers, config) {
$scope.title = "Oops... something went wrong";
$scope.working = false;
});
Here is my MVC method
[System.Web.Mvc.HttpPost]
private async void Post([FromBody] UserEntry userEntry)
{
if (userEntry.UserEntryID > 0)
{
// update
var dbUserEntry = this.db.UserEntries
.Where(u => u.UserEntryID == userEntry.UserEntryID)
.FirstOrDefault();
dbUserEntry.TeamName = userEntry.TeamName;
}
else {
// insert
this.db.UserEntries.Add(userEntry);
}
await this.db.SaveChangesAsync();
}
No matter what I try, I cannot access my Post method and keep getting the same error mentioned above.
Upvotes: 0
Views: 352
Reputation: 10851
Your method is private. Make it public and it should work much better.
As a side note, you may want to reconsider using async with void.
Prefer async Task methods over async void methods
https://msdn.microsoft.com/en-us/magazine/jj991977.aspx
Upvotes: 3
Reputation: 580
@David it seems like there is a confusion here as you have used/api/
which is default route for web api but you have mentioned you are using mvc.
It's web api if your controller is inherited from ApiController
. In that case System.Web.Http.Post
attribute would make the difference.
Though you have to make the method public to make it work in any case as @smoksnes mentioned.
Upvotes: 1