MVC - 请求的资源不支持http方法'POST'

时间:2022-09-11 13:29:38

I have read various posts around the message

我已阅读有关该消息的各种帖子

The requested resource does not support http method 'POST'

请求的资源不支持http方法'POST'

I have made sure I have included the Http attribute. I have tried both System.Web.HttpPost and System.Web.Mvc.HttpPost

我已经确定我已经包含了Http属性。我已经尝试过System.Web.HttpPost和System.Web.Mvc.HttpPost

I have also included the [FromBody] tag

我还包括[FromBody]标签

I have an angularjs page trying to post to a MVC method

我有一个angularjs页面试图发布到MVC方法

Here is my angularjs call

这是我的angularjs电话

$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

这是我的MVC方法

[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 cannot access my Post method and keep getting the same error mentioned above

无论我尝试什么都无法访问我的Post方法并继续得到上面提到的相同错误

Any help greatly appreciated

任何帮助非常感谢

2 个解决方案

#1


3  

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.

作为旁注,您可能需要重新考虑使用async和void。

Prefer async Task methods over async void methods

首选async任务方法优于异步void方法

https://msdn.microsoft.com/en-us/magazine/jj991977.aspx

https://msdn.microsoft.com/en-us/magazine/jj991977.aspx

#2


1  

@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.

@David似乎这里有一个混乱,因为你使用/ api /这是web api的默认路由,但你提到你使用的是mvc。如果你的控制器是从ApiController继承的,那就是web api。在那种情况下,System.Web.Http.Post属性会有所不同。虽然你必须公开这个方法,但无论如何都要像@smoksnes所说的那样使它工作。

#1


3  

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.

作为旁注,您可能需要重新考虑使用async和void。

Prefer async Task methods over async void methods

首选async任务方法优于异步void方法

https://msdn.microsoft.com/en-us/magazine/jj991977.aspx

https://msdn.microsoft.com/en-us/magazine/jj991977.aspx

#2


1  

@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.

@David似乎这里有一个混乱,因为你使用/ api /这是web api的默认路由,但你提到你使用的是mvc。如果你的控制器是从ApiController继承的,那就是web api。在那种情况下,System.Web.Http.Post属性会有所不同。虽然你必须公开这个方法,但无论如何都要像@smoksnes所说的那样使它工作。