• .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();
            }
    
        }
  • 相关阅读:
    laravel md5+salt 密码
    sql语句 当前时间查找重复 时间戳转换
    Supesite 参数说明
    ajax
    PDO操作
    laravel paginate动态分页
    PHP iconv()函数转字符编码的问题(转)
    计算几何基础模板(2014.10.6一直沿用)
    hdu 4893
    2014 ACM/ICPC 鞍山赛区网络赛(清华命题)
  • 原文地址:https://www.cnblogs.com/xdot/p/9629426.html
Copyright © 2020-2023  润新知