當呼叫 put、delete 方法時,我收到此錯誤 No 'Access-Control-Allow-Origin' 但 get, post 方法沒有錯誤,在服務器上發布時會發生這種情況,但在我的計算機上我沒有任何錯誤,這是代碼在啟動檔案中
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseAuthentication();
app.UseHttpsRedirection();
app.UseCors("corsPolicy");
app.UseStaticFiles();
app.UseMvc();
}
services.AddCors(options =>
{
options.AddPolicy("corsPolicy", builder =>
{
builder.AllowAnyMethod().AllowAnyOrigin().AllowAnyHeader().AllowCredentials();
});
});
控制器代碼
namespace ContinuationProjLast.Controllers
{
[Route("api/[controller]")]
[ApiController]
[EnableCors("corsPolicy")]
public class marksController : ControllerBase
{
private readonly ProductionRefContext _context;
public marksController(ProductionRefContext context)
{
_context = context;
}
[HttpPut("{id}")]
public async Task<IActionResult> Putmark(int id, mark mark)
{
if (id != mark.ID)
{
return BadRequest();
}
var mm = _context.mark.Where(x => x.devicemark == mark.devicemark && x.devicecategory == mark.devicecategory).FirstOrDefault();
if (mm == null)
{
_context.Entry(mark).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!markExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
else
{
return BadRequest();
}
}
delete 方法也會發生同樣的事情。
我能得到任何幫助嗎?
uj5u.com熱心網友回復:
iis 服務器本質上不支持 put http 動詞,需要做一些額外的設定。我將它們更改為 post 動詞并在生產中運行良好
uj5u.com熱心網友回復:
我從 iis 中洗掉了 WebDAV 發布,轉到控制面板 > 程式和功能 > 打開或關閉 Windows 功能 > IIS > 萬維網服務 > 通用 HTTP 功能 > WebDAV 發布
問題已解決
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/312533.html
上一篇:為什么Axios會在我的POST請求中發送一個額外的204預檢請求?
下一篇:ssl與post請求的通信問題
