简体   繁体   English

URL Angular2 +打字稿Webapp不允许使用405方法

[英]405 Method Not Allowed for URL angular2+ typescript webapp

I am developing an angular2+ app with typescript. 我正在使用打字稿开发angular2 +应用程序。 I am trying to pull data for one of my Angular2 Kendo grid and making the following call to get results to populate the grid. 我试图为我的Angular2 Kendo网格之一提取数据,并进行以下调用以获取结果以填充网格。 In this web call I am sending a status ID , skip and take and a Sort array which contains the field I want to sort and the direction I want it sorted in. 在此网络电话中,我将发送状态ID,跳过并获取和一个Sort数组,其中包含要排序的字段以及排序的方向。

Below is my typescript code which makes the web service call: 下面是我的打字稿代码,它可以调用Web服务:

  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
            }));

    }

This is my service side code which never gets hit. 这是我的服务端代码,永远不会被击中。

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

Problem I am facing: For some reason when make this web service call I get an error saying 我面临的问题:由于某种原因,在进行此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 “错误,状态响应:405 URL不允许使用方法: http:// localhost / Services / RequestService / api / Dashboard / GetUserRequestsByStatuses ”我检查了正在发送的请求对象,这看起来像

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

Also in the response body I see this message: 同样在响应正文中,我看到以下消息:

{"Message":"The requested resource does not support http method 'POST'."} {“消息”:“请求的资源不支持http方法'POST'。”}

I been looking at this since Friday and could not come up with a solution. 自星期五以来,我一直在研究此问题,因此无法提出解决方案。 Any help is appreciated. 任何帮助表示赞赏。 I think it may be something to do with how I am sending my request object through typescript. 我认为这可能与我通过打字稿发送请求对象有关。

The error says the method POST is not supported yet the action has a HttpGet attribute. 该错误表明不支持POST方法,但该操作具有HttpGet属性。

You'll need to change the HttpGet to HttpPost . 您需要将HttpGet更改为HttpPost

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

You could also lose the [FromBody] attribute too, it's the default given the parameter type. 您也可能会丢失[FromBody]属性,这是给定参数类型的默认属性。

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

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