IIS下网站对options请求直接返回404

什么是options请求
options请求为发送非简单跨域请求前的预检请求,若该请求未正常返回,浏览器会阻止后续的请求发送。
一般情况下,有三种方式会导致浏览器发起预检请求

1.请求的方法不是GET/HEAD/POST
2.POST请求的Content-Type并非application/x-www-form-urlencoded, multipart/form-data 或 text/plain
3.请求中设置了自定义的header字段(如Token)

浏览器发出请求但直接返回404
若未对iis进行配置,则会导致options请求被iis直接响应返回,而不会进入到代码中。这也是Global中的Application_BeginRequest无法捕获到options请求的原因。
1.检查webconfig中的配置,是否移除了对options请求的特殊处理
可在iis中进行配置:[网站]-[应用程序]-[处理程序映射]

<system.webServer>



</system.webServer>
2.检查iis服务器是否安装了UrlScan,若安装了请检查AllowVerbs中是否包含了options
可在iis中查看是否安装了UrlScan [网站]-[ISAPI筛选器] (可以找到UrlScan安装路径)
image

UrlScan的配置文件为UrlScan.ini (C:\Windows\System32\inetsrv\urlscan\UrlScan.ini)
将OPTIONS从[DenyVerbs]中移除并添加到[AllowVerbs]下

3.在Global的Application_BeginRequest实践中直接响应options请求

//允许所有的options请求,直接返回200状态码
private void Application_BeginRequest(object sender, EventArgs e)
{
if (HttpContext.Current.Request.HttpMethod == “OPTIONS”)
{
HttpContext.Current.Response.StatusCode = 200;
HttpContext.Current.Response.Headers[“Access-Control-Allow-Origin”] = HttpContext.Current.Request.Headers[“origin”];
HttpContext.Current.Response.End();
}
}
4.在webconfig中的Allow-Method中添加上options

<system.webServer>







</system.webServer>
深圳网站建设 https://www.sz886.com/

猜你喜欢

转载自blog.csdn.net/weixin_45032957/article/details/90106373