.net IIS MVC Rest api 跨域 PUT DELETE 404 无法使用问题解决方案
一、WebConfig配置法(system.webServer 重点是 httpProtocol handlers)
http://www.jinxuliang.com/blog/article/read/3348d442-9432-49d8-abe9-2c64ce6436cd
http://www.matlus.com/rest-apis-put-and-delete-cause-http-error-404/ *
2个内容配合使用 ,第二个连接的方法,会自动改Web.config
<system.webServer> <!--IIS7/7.5上必须加这个配置,否则访问报错--> <modules runAllManagedModulesForAllRequests="true"> <remove name="WebDAVModule"/> </modules> <validation validateIntegratedModeConfiguration="false"/> <!--以下配置为了让IIS7+支持Put/Delete方法(CORS)--> <httpProtocol> <customHeaders> <!--<add name="Access-Control-Allow-Origin" value="http://*.com" />--> <!--<add name="Access-Control-Allow-Origin" value="http://local*:49356" />--> <add name="Access-Control-Allow-Methods" value="GET,POST,PUT,DELETE,PATCH,OPTIONS"/> <add name="Access-Control-Allow-Headers" value="Accept,Content-Type,X-Requested-With"/> <add name="Access-Control-Allow-Credentials" value="true"/> <!-- 跨域session保持 --> </customHeaders> </httpProtocol>
<handlers> <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit"/> <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit"/> <remove name="WebDAV"/> <remove name="OPTIONSVerbHandler"/> <!--跨域重点配置--> <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0"/> <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0"/> </handlers>
</system.webServer>
***************
<remove name="OPTIONSVerbHandler"/>
二、终级Global.asax代码处理方法
//cros 跨域请求配置 protected void Application_BeginRequest(object sender, EventArgs e) { string origin = HttpContext.Current.Request.Headers["Origin"]; if (origin != null) { HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", origin); } else { HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); } if (HttpContext.Current.Request.HttpMethod == "OPTIONS" && Request.RawUrl.ToLower().Contains(".asmx/")) { HttpContext.Current.Response.End(); } }