【发布时间】:2020-02-10 22:51:17
【问题描述】:
我正在尝试从我得到 Cross-Origin Request Blocking: The "Same Origin" policy does not allow access to the remote resource located at http://localhost:8888/api/users/17. Reason: Missing method in the "Access-Control-Allow-Methods" header 的角度服务中调用在 ASP.NET 中实现的 PUT web api
我能够毫无问题地从角度调用 GET 和 POST 方法
源代码:
edit(user : User) : Observable<object> {
his.httpClient.put('http://localhost:8888/api/users/' + user.userId, user);
}
这里是 PUT web api:
[HttpPut("{id}")]
public async Task<ActionResult> Put(int id, [FromBody] UserViewModel model)
{
try
{
var user = _mapper.Map<UserViewModel, User>(model);
var status = await _repository.UpdateUserAsync(user);
if (!status)
{
return BadRequest("failed to edit user");
}
return Ok(_mapper.Map<User, UserViewModel>(user));
}
catch (Exception exp)
{
_loger.LogError(exp.Message);
return BadRequest("failed to edit user");
}
}
我还在启动类的服务中添加了 Cors,如下所示:
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
【问题讨论】:
标签: angular asp.net-core asp.net-web-api cors