zoukankan      html  css  js  c++  java
  • .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.NETFrameworkv4.0.30319aspnet_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.NETFramework64v4.0.30319aspnet_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();
            }
    
        }
  • 相关阅读:
    快速排序
    推荐!手把手教你使用Git
    「瞻前顾后」写出我心(九十九)
    「减法」写出我心(九十八)
    「焦虑」写出我心(九十七)
    「认知水平」​​​​​​​​写出我心(九十六)
    「成功的事业」写出我心(九十五)
    「爱」​​​​​​写出我心(九十四)
    「赢」​​​​​写出我心(九十三)
    「体面人」​​​​写出我心(九十二)
  • 原文地址:https://www.cnblogs.com/xdot/p/9629426.html
Copyright © 2011-2022 走看看