繁体   English   中英

带参数的WebApi 404-通过javascript GET调用

[英]WebApi 404's with Parameters - calling via javascript GET

LocationsController中的WebApi方法:

public async Task<IHttpActionResult> GetLocations(int id, long start, long end)
{
return Ok();
}

我尝试拨打的电话(GET):

https://api.com/Locations/GetLocations/34934/1/1
https://api.com/Locations/GetLocations?34934&start=1&end=1

因此:

factHttp.httpRequest('/locations/GetLocations', 'GET', vm.ID + '/' + start.getTime() + '/' + now.getTime()).then(function (_result) {
                vm.plots = angular.fromJson(_result.data);
            });

var _httpRequest = function requestCall(requestPartURL, requestType, requestPayload) {

            var _resultHttp = $http({
                url: apiURL + requestPartURL,
                method: requestType,
                data: requestPayload,
                headers: {
                    'Authorization': 'Bearer ' + localStorage.getItem('ACCESS_TOKEN')
                }
            }).success(function (response) {
                console.log("HTTP CALL SUCCESS");
                return {
                    result: 'SUCCESS',
                    resultObj: response,
                    errorCode: '',
                    errorDesc: ''
                }

            }).error(function (response) {
                console.log("HTTP CALL FAIL");
                console.log(response);

                return getErrorObj(response);
            });

            return _resultHttp;

        }

路由:

config.Routes.MapHttpRoute(
            "IdentWithRange",
            "{controller}/{action}/{id}/{start}/{end}"
        );

如果我将Web api方法更改为仅接受(int id)并调用GetLocation / 34934,则效果很好。 我在这里做错了什么?

我不断收到404错误。 其中一些不是我自己编写的(即我不太擅长写JS的东西),但这是我第一次使用任何类型的api,我控制两端,但有点卡住了。

您创建的路由记录将寻找以下路由

/api/controller/id/start/end

但您尝试传递参数,而不是指定路线。

您不需要特殊的路由记录即可处理多个参数。 使用“标准”路由记录就足够了。

config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}", new 
{ 
  id = RouteParameter.Optional, action = "DefaultAction" 
});

当前,您需要在路线中添加“ api”。

https://api.com/api/Locations/GetLocations/34934/1/1

推荐的:

https://api.com/api/locations/34934/1/1

[HttpGet, Route("locations/{id:int:min(1)}/{start:long}/{end:long}")]
public async Task<IHttpActionResult> GetLocations(int id, long start, long end)
{
    return Ok();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM