角5; asp.net web api http.delete不起作用

时间:2018-06-16 05:58:16

标签: asp.net-web-api angular5

我有asp.net web api服务器在调试中运行,单独的项目使用Angular 5 - 5.2.11。

我尝试删除数据但我不能,但是当我使用邮递员时,它可以正常工作。

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { map } from 'rxjs/operators';

@Injectable()
export class UserService { private getUserSucess: any;

  private  _headers =  {
    headers: new HttpHeaders().set('Content-Type', 'application/json')
  };

  private headers={
    headers: new HttpHeaders({
   'Content-Type': 'application/json'
    });
  }

  constructor(private http: HttpClient) { }

  deletUserHttp() {
    return this.http.delete( 'http://localhost:52817/api/users/1' ).subscribe(deleteSucess.bind(this), deleteError.bind(this));


 function deleteSucess(resp) {
   debugger;
   return resp;
 }
 function deleteError(resp) {
  //resp = HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: null, ok: false, 
  debugger;
  return resp;
 }
}

我也尝试使用:this._headersthis.headars结果相同:

  

HttpErrorResponse {headers:HttpHeaders,status:0,statusText:" Unknown Error",url:null,ok:false,

     

405方法不允许:

     

请求网址:http://localhost:52817/api/users/1
     请求方法:选项
     状态代码:405不允许的方法
     远程地址:[:: 1]:52817
     推荐人政策:no-referrer-when-downgrade
     访问控制允许方法:GET,POST,PUT,DELETE,OPTIONS
     Access-Control-Allow-Origin:*
     允许:GET,PUT,DELETE
     缓存控制:无缓存
     内容长度:71
     Content-Type:application / json;字符集= UTF-8
     日期:星期六,2018年6月16日05:43:07 GMT
     到期:-1
     Pragma:no-cache
     服务器:Microsoft-IIS / 10.0
     X-AspNet-Version:4.0.30319
     X-Powered-By:ASP.NET
     X-SourceFiles:=?UTF-8?B?
       QzpcVXNlcnNcQmV0bWlyYVxzb3VyY2VccmVwb3NcVXNlcnNcVXNlcnNcYXBpXHVzZXJzXDE =?=      接受: /
     Accept-Encoding:gzip,deflate,br
     Accept-Language:pl-PL,pl; q = 0.9,en-US; q = 0.8,en; q = 0.7,de; q = 0.6
     访问控制请求标题:内容类型
       访问控制请求方法:删除        连接:保持活力        主持人:localhost:52817        来源:http://localhost:4200        User-Agent:Mozilla / 5.0(Windows NT 6.1; Win64; x64)AppleWebKit / 537.36(KHTML,与Gecko一样)Chrome / 67.0.3396.87 Safari / 537.36

web.config

<system.webServer>
    <httpProtocol>
        <customHeaders>
            <add name="Access-Control-Allow-Origin" value="*" />       
            <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />    
        </customHeaders>
    </httpProtocol>
    <rewrite>
        <outboundRules>
            <clear />
            <rule name="AddCrossDomainHeader">
                <match serverVariable="RESPONSE_Access_Control_Allow_Origin" pattern=".*" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="true">
                    <add input="{HTTP_ORIGIN}" pattern="(http(s)?://((.+\.)?domain1\.com|(.+\.)?domain2\.com|(.+\.)?domain3\.com))" />
                </conditions>
                <!--<match serverVariable="RESPONSE_Access-Control-Allow-Methods" pattern="GET, POST, PUT, DELETE, OPTIONS" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="true">
                    <add input="{HTTP_ORIGIN}" pattern="(http(s)?://((.+\.)?domain1\.com|(.+\.)?domain2\.com|(.+\.)?domain3\.com))" />
                </conditions>-->
                <action type="Rewrite" value="{C:0}" />
            </rule>
        </outboundRules>
    </rewrite>
...

请帮助我,因为我真的不知道出了什么问题。

1 个答案:

答案 0 :(得分:0)

请参阅此document以获取asp.net webapi的CORS。启用CORS后,您应该可以使用Angular app删除。