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'? –