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

MVC - The requested resource does not support http method 'POST'

我已阅读有关消息的各种 post

The requested resource does not support http method 'POST'

我已确保包含 Http 属性。 System.Web.HttpPostSystem.Web.Mvc.HttpPost

我都试过了

我还添加了 [FromBody] 标签

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

这是我的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;
});

这是我的 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();
}

无论我尝试什么,我都无法访问我的 Post 方法并不断出现上述相同的错误。

@David 似乎这里有一个混淆,因为你使用了 /api/ 这是 web api 的默认路由,但你提到了你正在使用 mvc.
如果您的控制器继承自 ApiController,则它是网络 api。在那种情况下,System.Web.Http.Post 属性会有所不同。
尽管您必须使方法 public 使其在任何情况下都能正常工作,正如@smoksnes 提到的那样。

您的方法是私有的。 让它成为 public,它应该会更好。

附带说明一下,您可能需要重新考虑将 async 与 void 结合使用。

Prefer async Task methods over async void methods

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