Bob Horn Bob Horn - 1 month ago 7
C# Question

HttpGet Attribute Required for One Method But Not Another

I'm making a Web API call, and I'm getting this error:


405 Method Not Allowed
The requested resource does not support http
method 'GET'.


Here is the call:

var config = {
url: rootWebApiUrl + '/api/containerMove/allowMultipleBoxesPerMove',
method: 'GET'
};

$http(config)
.then(function (response) {
// code here
}, function (response) {
// code here
});


If I add the HttpGet attribute to the Web API method, it works:

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


What I don't understand is that
HttpGet
isn't needed for other calls I'm making on the same Web API controller. Here's one that works without the
HttpGet
attribute:

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

$http(config)


And the Web API method:

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


So why do I need
HttpGet
on one Web API method but not the other? Those calls and API methods are just about identical.

Answer

Bob, Web API has a paradigm that is convention over configuration, so, in this case, all the actions that the name begins with Get will be asociated to a HTTP Get, this is the reason that getBatchRefreshInterval does not need the attribute [HttpGet]