0
我想创建一个从我的角4应用程序调用我的服务器是asp.net web api,首先我启用cors我的服务器和它似乎工作,现在当我试图打电话给我收到方法不被允许方法不允许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());
}
我也试过头内容类型:应用程序/ json也没有工作。
其工作的罚款,如果我做了邮递员的请求......
感谢。
'返回this.http.get(this.url,...' - 了'http'在那里,是它'HttpClientModule'或'HttpModule'? –