2017-10-10 18 views
0

내 Angular 4 애플리케이션에서 asp.net 웹 API 인 서버에 대한 호출을 만들려고합니다. 내가 허용되지 않는 방법을 얻고 호출하려고 할 때 내 서버와 지금, 작업 보인다메서드가 허용되지 않습니다 405 애트리뷰트 httpclient에서 asp.net 웹 API로 호출하려고 시도 할 때

WebApi.config :

public static void Register(HttpConfiguration config) 
{ 
    var cors = new EnableCorsAttribute("*", "*", "*"); 
    config.EnableCors(cors); 
    // Web API configuration and services 
    // Configure Web API to use only bearer token authentication. 
    config.SuppressDefaultHostAuthentication(); 
    config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType)); 

    // Web API routes 
    config.MapHttpAttributeRoutes(); 



    config.Routes.MapHttpRoute(
     name: "DefaultApi", 
     routeTemplate: "api/{controller}/{id}", 
     defaults: new { id = RouteParameter.Optional } 
    ); 
} 

컨트롤러 방법 예 :

 [HttpGet, Route("api/authentication")] 
    public IHttpActionResult Get(string username, string password) 
    { 
     var s = new ApartmentService(); 

     if (!s.CheckIfValidLogin(username, password)) 
     { 
      return NotFound(); 
     } 
     return Ok(); 
    } 

클라이언트 코드 :

public url = 'http://localhost:50743/api/authentication'; 

    login(username: string, password: string): Observable<any> { 
    let params = new HttpParams(); 
    params.append('username', username); 
    params.append('password', password); 
    return this.http.get(this.url, { 
     params: params, 
     headers: new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'), 
    }).map((result: Response) => result.json()); 
    } 

또한 헤더 내용 유형도 시도했습니다. application/json 또한 작동하지 않았습니다.

오류 : 나는 우체부와 요청 ...

감사를 한 경우에 error

는 잘 작동합니다.

+0

을 참조하십시오 ', this.http.get (this.url를 반환 ...'- (가)'http' 거기에서, 그것은'HttpClientModule' 또는'HttpModule'입니까? –

답변

0

GET 요청을 POST 만 허용하는 엔드 포인트에 보내려고하고 있는데, 이것이 Method Not Allowed 오류의 원인입니다.

대신 POST을 사용하여 요청을 보냅니다. 나는 각도에 익숙하지 않아요하지만

login(username: string, password: string): Observable<any> { 
    let params = new HttpParams(); 
    params.append('username', username); 
    params.append('password', password); 
    return this.http.post(this.url, parems { 
     headers: new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'), 
    }).map((result: Response) => result.json()); 
} 

같은 것을 시도 angular docs for http.post