ASP Net Core API- PUT方法
我正在努力使API PUT方法正常工作,同时消费了我的API(托管在Plesk)中,这是从Glazor WebAssembly(.NET6)中使用的。
我已经有get和post方法正常工作,并且已经设置了我的CORS策略(allowanyorigins
,allowAnymethod
,allowaneheader
),但仍会遇到错误: “如果我直接在Web API(Swagger)中尝试,则在请求的资源>和
405
上没有'访问控制 - 遵守孔“孔”标头。 有什么想法吗?
这是Swagger的答复:
允许:获取,头,选项,跟踪,put,发布内容长度: 104内容类型:文本/html日期:THU,2022年5月5日12:29:43 GMT
服务器:Microsoft-iis/10.0通过:代理X-FireFox-Spdy:H2 X-Power-By:Asp.net X-Powered-By-persek:Pleskwin
请求url url url
https:// ********* ***。it/api/requinisitions/62
这是控制器:
[HttpPut("{id}")]
public async Task<IActionResult> UpdateRequisitionAsync(int id, [FromBody] RequisitionDTO requisitionFromUI)
{
if (!ModelState.IsValid)
{
throw new BadHttpRequestException("Error in the requisition");
}
else
{
var requisitionInDb = await _context.Requisitions.SingleOrDefaultAsync(a => a.Id == requisitionFromUI.Id);
if (requisitionInDb != null)
{
requisitionInDb.PriceCurr = requisitionFromUI.PriceCurr;
requisitionInDb.PurchaseQty = requisitionFromUI.PurchaseQty;
requisitionInDb.WantedReceiptDate = requisitionFromUI.WantedReceiptDate;
requisitionInDb.PartDescription = requisitionFromUI.PartDescription;
requisitionInDb.RequisitionNote = requisitionFromUI.RequisitionNote;
await _context.SaveChangesAsync();
return Ok(requisitionFromUI);
}
return NotFound();
}
}
in Program.cs:
builder.Services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
{
builder.AllowAnyMethod().AllowAnyOrigin().AllowAnyHeader();
}));
var app = builder.Build();
请求管道:
app.UseSwagger();
if (!app.Environment.IsDevelopment())
{
app.UseSwaggerUI(c => {
c.SwaggerEndpoint("/swagger/v1/swagger.json", "myapi v1");
c.RoutePrefix = String.Empty;
});
}
else
{
app.UseSwaggerUI(c => {});
}
app.UseHttpsRedirection();
app.UseCors("MyPolicy");
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();
I'm struggling to have the API PUT method working correctly while consuming my api (hosted on Plesk) from a blazor webassembly (.net6).
I Already have the GET and POST method working fine and already set my cors policy (AllowAnyOrigins
,AllowAnyMethod
,AllowAnyHeader
) but still getting the error: "No 'Access-Control-Allow-Origin' header is present on the requested resource"
and 405
if i try directly in the web api (swagger).
Any ideas?
here is the reply from swagger:
allow: GET,HEAD,OPTIONS,TRACE,PUT,POST content-length:
104 content-type: text/html date: Thu,05 May 2022 12:29:43 GMT
server: Microsoft-IIS/10.0 via: Proxy x-firefox-spdy: h2 x-powered-by: ASP.NET x-powered-by-plesk: PleskWin
request url
https://************.it/api/Requisitions/62
here is the controller:
[HttpPut("{id}")]
public async Task<IActionResult> UpdateRequisitionAsync(int id, [FromBody] RequisitionDTO requisitionFromUI)
{
if (!ModelState.IsValid)
{
throw new BadHttpRequestException("Error in the requisition");
}
else
{
var requisitionInDb = await _context.Requisitions.SingleOrDefaultAsync(a => a.Id == requisitionFromUI.Id);
if (requisitionInDb != null)
{
requisitionInDb.PriceCurr = requisitionFromUI.PriceCurr;
requisitionInDb.PurchaseQty = requisitionFromUI.PurchaseQty;
requisitionInDb.WantedReceiptDate = requisitionFromUI.WantedReceiptDate;
requisitionInDb.PartDescription = requisitionFromUI.PartDescription;
requisitionInDb.RequisitionNote = requisitionFromUI.RequisitionNote;
await _context.SaveChangesAsync();
return Ok(requisitionFromUI);
}
return NotFound();
}
}
in program.cs:
builder.Services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
{
builder.AllowAnyMethod().AllowAnyOrigin().AllowAnyHeader();
}));
var app = builder.Build();
the request pipeline:
app.UseSwagger();
if (!app.Environment.IsDevelopment())
{
app.UseSwaggerUI(c => {
c.SwaggerEndpoint("/swagger/v1/swagger.json", "myapi v1");
c.RoutePrefix = String.Empty;
});
}
else
{
app.UseSwaggerUI(c => {});
}
app.UseHttpsRedirection();
app.UseCors("MyPolicy");
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定,但是我认为可以为您提供帮助。您需要在API的启动中添加允许的标头列表:
另外,您需要指定允许使用访问控制的起源。要么只允许原点,要么允许其他域或任何原点。
您的httpcontext是上下文。在错误消息或您的信息消息中,当您的请求因前飞行而被拒绝时收到的信息消息,它应该告诉您不允许什么标头。
cross-origin资源共享
在这里阅读 href =“ https://learn.microsoft.com/en-us/aspnet/core/core/fundamentals/middleware/?view = ateaspnetcore-6.0“ rel =“ nofollow noreferrer”
I am not sure, but here goes what I think could help you. You need to add allowed list of headers to your reponse in your API's startup:
Also, you need to specify origins you allow using the Access-Control-Allow-Origin. Either only the origin is allowed, or another domain can be allowed or any origin.
Where context is your HttpContext. In the error message, or information message you get when your request is rejected because of preflight, it should tell you what header is not allowed.
Read here Cross-Origin Resource Sharing
Also read here about Middleware delegates
我能够对其进行修改Web.config文件的操作:
BU我想让Visual Studio正确生成文件,而不是修复每个API发布/修改。
I was able to do it amending the web.config file as follow:
bu I'd like to have Visual Studio generating the file correctly and not fixing this every API publish/modifications.