【问题标题】:WebApi 404's with Parameters - calling via javascript GET带参数的 WebApi 404 - 通过 javascript GET 调用
【发布时间】:2015-11-24 10:24:59
【问题描述】:

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,我控制两端,我有点卡住了。

【问题讨论】:

  • 你试过了吗:http://api.com/Locations/GetLocations?id=34934&amp;start=1&amp;end=1
  • 刚刚试了一下,没用,很遗憾。它甚至根本没有命中方法,只是直接向上 404s。
  • 为了好玩,您可以尝试将您的 MapHttpRoute 从“IdentWithRange”重命名为“DefaultApi”吗?
  • 这破坏了 API 其余部分的一切:D
  • 好的。我认为问题出在您的路由中。假设您有一个看起来像这样的“DefaultApi”; config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}", new { id = RouteParameter.Optional, action = "DefaultAction" });,您可以尝试删除您的“IdentWithRange”吗?

标签: javascript c# parameters routing asp.net-web-api


【解决方案1】:

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

/api/controller/id/start/end

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

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

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

【讨论】:

    【解决方案2】:

    目前您需要在路由中添加“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();
    }
    

    【讨论】:

    • 我首先将其更改为您在顶部显示的内容,然后再次更改为您在底部显示的内容,省略了动作部分。我仍然得到 404'd 并且无法弄清楚为什么 puu.sh/lx4Fx/e172d36edc.png
    • 在您的图像中,您没有传递 id。因此它将无法找到您的路线。
    • 那么这可能是javascript中的问题,我根本不习惯所以我会尝试找出更多。
    • $http.get('api.com/api/locations/getvehiclelocations' + vm.selectedVehicleId + '/' + start.getTime() + '/' + now.getTime(), { headers: { 'Authorization' : '承载' + localStorage.getItem('ACCESS_TOKEN') } });这也会为我返回 404:puu.sh/lx9zg/8989dbfa8d.png
    • 您的呼叫是“getvehiclelocations”,但您的路线是“getlocations”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    • 2015-08-02
    相关资源
    最近更新 更多