URL angular 2 打字稿网络应用程序不允许使用 405 方法

405 Method Not Allowed for URL angular2+ typescript webapp

我正在使用 typescript 开发一个 angular2+ 应用程序。我正在尝试为我的 Angular2 Kendo 网格之一提取数据并进行以下调用以获取填充网格的结果。在此网络调用中,我发送了一个状态 ID、skip 和 take 以及一个 Sort 数组,其中包含我要排序的字段和我希望它排序的方向。

下面是我调用网络服务的打字稿代码:

  getUserRequests(statusId: number, skip: number, take: number, sort: SortDescriptor[]): Observable<GridResult> {
                     private getUserRequestsUrl = environment.serviceHostName + environment.serviceAppURL + '/api/Dashboard/GetUserRequestsByStatuses';

        var headers = new Headers();
        headers.append('Content-Type', 'application/json;');

        return this.http.post(this.getUserRequestsUrl, JSON.stringify({ "Filter": { "Field": "StatusId", "Value": statusId, "Operator": "eq" },
 "Skip": skip, "Take": take, "Sort": sort }), { headers: this.headers }).share()
            .map((res: Response) => res.json())
            .map(response => (<GridResult>{
                data: response.Data ,
                total: response.Count
            }));

    }

这是我的服务端代码,永远不会被命中。

[HttpGet]
public HttpResponseMessage GetUserRequestsByStatuses([FromBody] DataRequest model)
{
    DataResponse<AngKendoGridDashboard> response =
        BusinessAccess.GetUserRequestsByStatuses(model);
    return CreateHttpResponse(response);
}

我面临的问题: 出于某种原因,在进行此 Web 服务调用时我收到一条错误消息

"ERROR Response with status: 405 Method Not Allowed for URL: http://localhost/Services/RequestService/api/Dashboard/GetUserRequestsByStatuses" I checked the request object which is being sent and here is what is looks like

{"Filter":{"Field":"StatusId","Value":2,"Operator":"eq"},"Skip":0,"Take":5,"Sort":[{"field":"CreatedDate","dir":"desc"}]}

我还在响应正文中看到这条消息:

{"Message":"The requested resource does not support http method 'POST'."}

我从周五开始就一直在研究这个问题,但一直想不出解决办法。任何帮助表示赞赏。我认为这可能与我如何通过打字稿发送请求对象有关。

错误表明方法 POST 不受支持,但操作具有 HttpGet 属性。

您需要将 HttpGet 更改为 HttpPost

[HttpPost]
public HttpResponseMessage GetUserRequestsByStatuses([FromBody] DataRequest model)
{
    DataResponse<AngKendoGridDashboard> response = 
        BusinessAccess.GetUserRequestsByStatuses(model);
    return CreateHttpResponse(response);
}

您也可能会丢失 [FromBody] 属性,它是给定参数类型的默认值。