• 跨域问题总结


    1.ASP.NET WebApi 跨域(Cors)配置

    第一步:下载nuget包

    Install-Package Microsoft.AspNet.WebApi.Cors

    第二步:在webApiConfig 中配置代码如下

    public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
                // Web API 配置和服务
     
                // Web API 跨域配置  引入跨域nuget包:Microsoft.AspNet.WebApi.Cors
                string cors_hosts = ConfigurationManager.AppSettings["cors_hosts"];
                if (!string.IsNullOrEmpty(cors_hosts))
                {
                    var allowOrigins = cors_hosts;//最好来自配置文件夹 "*"
                    var allowHeaders = "*";//最好来自配置文件夹
                    var allowMethods = "*";//最好来自配置文件夹
                    var globalCors = new System.Web.Http.Cors.EnableCorsAttribute(allowOrigins, allowHeaders, allowMethods)
                    {
                        SupportsCredentials = true,
                        // 设置预检查时间
                        PreflightMaxAge = 20 * 60,
                    };
                    config.EnableCors(globalCors);
                }
     
                // Web API 路由
                config.MapHttpAttributeRoutes();
     
                // Web API 全局异常捕捉
                config.Filters.Add(new ExceptionAPIFilter());
     
                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{action}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );
            }   
    }
    

    最后在web.config配置文件中appSettings节点中配置允许访问的路径

    <appSettings>
        <!--支持跨域访问的域名,注意端口是必须的(除80),结尾不能加“/“-->
        <add key="cors_hosts" value="http://192.168.9.8:9527,http://192.168.9.176:9528,http://192.168.9.246:9529"/>
    </appSettings>
    

    2. .net mvc webapi设置允许跨域

    global.asax中    
       //解决跨域问题
            protected void Application_BeginRequest(object sender, EventArgs e)
            {
                //RegisterRoutes(RouteTable.Routes);
                if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
                {
                    HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
                    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
                    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
                    HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
                    HttpContext.Current.Response.End();
                }
                else
                {
                    HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
    
                    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST,PUT, DELETE");
                    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type");
                }
            }
    
    web.config中
    <!--解决跨域问题-->
        <httpProtocol>
          <customHeaders>
            <add name="Access-Control-Allow-Origin" value="*" />
            <add name="Access-Control-Max-Age" value="30" />
            <add name="Access-Control-Allow-Methods" value="GET,POST,OPTIONS" />
            <add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
          </customHeaders>
        </httpProtocol>
    
    iis:
    网站的应用程序池 管道模式必须是经典模式 ,否则不行
    

    3.Asp.Net WebApi 跨域设置

    https://www.cnblogs.com/webapi/p/10542547.html

    “fool me once,shame on you. fool me twice, shame on me.”,翻译过来的意思是“愚弄我一次,是你坏;愚弄我两次,是我蠢”。
  • 相关阅读:
    .Net Core ----通过XUnit进行接口单元测试(带请求头及参数)并用output输出结果
    .Net Core---- 通过EPPlus批量导出
    .Net Core---- 自带Json返回日期带T格式 解决
    You need tcl 8.5 or newer in order to run the Redis test
    exec: "docker-proxy": executable file not found in $PATH
    docker 如何清理垃圾呢
    docker run 报错——WARNING: IPv4 forwarding is disabled. Networking will not work.
    go 依赖包管理工具gb安装报错
    keepalived实现nginx高可用
    php命令行查看扩展信息
  • 原文地址:https://www.cnblogs.com/newcapecjmc/p/14493122.html
Copyright © 2020-2023  润新知