0
私のAngular 4アプリケーションからasp.net web apiであるサーバーにコールを作成しようとしています。私は許可されていないメソッドを取得しています呼び出ししようとしていたときに私のサーバーとそれが今、働いているようだメソッドが許可されていない405角度httpclientからasp.net Web 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で試してみましたが、うまくいきませんでした。
私は郵便配達してリクエストをした場合、その罰金働いて...
感謝。
を参照してください '、this.http.get(this.urlを返す...' - 'http'はそこに、ありますそれは 'HttpClientModule'か' HttpModule'ですか? –