一个方法需要HttpGet属性,而另一个方法不需要

HttpGet Attribute Required for One Method But Not Another

本文关键字:方法 属性 不需要 另一个 HttpGet 一个      更新时间:2023-09-26

我正在做一个Web API调用,我得到这个错误:

405方法不允许
请求的资源不支持http方法得到的。

电话:

    var config = {
        url: rootWebApiUrl + '/api/containerMove/allowMultipleBoxesPerMove',
        method: 'GET'
    };
    $http(config)
        .then(function (response) {
            // code here
        }, function (response) {
            // code here
        });

如果我添加HttpGet属性到Web API方法,它工作:

[HttpGet]
[Route("api/containerMove/allowMultipleBoxesPerMove")]
public bool AllowMultipleBoxesPerMove()

我不明白的是,HttpGet不需要我在同一个Web API控制器上进行的其他调用。下面是一个没有HttpGet属性的例子:

        var config = {
            url: rootWebApiUrl + '/api/containerMove/getBatchRefreshInterval',
            method: 'GET'
        };
        $http(config)

和Web API方法:

[Route("api/containerMove/getBatchRefreshInterval")]
public int GetBatchRefreshInterval()

那么为什么我需要一个Web API方法上的HttpGet而不是另一个?这些调用和API方法几乎是相同的。

Bob, Web API有一个约定优于配置的范例,因此,在这种情况下,名称以Get开头的所有操作将与HTTP Get相关联,这就是 Get BatchRefreshInterval不需要属性[HttpGet]

的原因