zoukankan      html  css  js  c++  java
  • .NET 跨域问题

    解决方案:

    1、针对ASP.NET MVC和ASP.NET Web API两种项目类型,我做了一些研究,确定下面的方案是可行的。

    针对ASP.NET MVC,只需要在web.config中添加如下的内容即可

     1 <system.webServer>
     2 
     3 <httpProtocol>
     4 
     5 <customHeaders>
     6 
     7 <add name="Access-Control-Allow-Origin" value="*" />
     8 
     9 <add name="Access-Control-Allow-Headers" value="Content-Type" />
    10 
    11 <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
    12 
    13 </customHeaders>
    14 
    15 </httpProtocol>
    16 
    17 <handlers>
    18 
    19 <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
    20 
    21 <remove name="OPTIONSVerbHandler" />
    22 
    23 <remove name="TRACEVerbHandler" />
    24 
    25 <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    26 
    27 </handlers>
    28 
    29 </system.webServer>

     2、建立Filters,cors

     1 public class CorsAttribute : ActionFilterAttribute, IActionFilter
     2     {
     3         public override void OnResultExecuted(ResultExecutedContext filterContext)
     4         {
     5             try
     6             {
     7                 base.OnResultExecuted(filterContext);
     8                 HttpContext.Current.Response.Headers.Add("Access-Control-Allow-Origin", "*");
     9                 HttpContext.Current.Response.Headers.Add("Access-Control-Allow-Headers", "x-requested-with,content-type,requesttype,Token");
    10                 HttpContext.Current.Response.Headers.Add("Access-Control-Allow-Methods", "POST,GET");
    11             }
    12             catch (Exception ex)
    13             {
    14 
    15             }
    16         }
    17     }
    View Code

    页面调用:

    1 [Filters.Cors]
    2 public ActionResult Index()
    3 {
    4       return view();  
    5 }
  • 相关阅读:
    面试题目以及注意事项
    jQuery Ajax 实例 ($.ajax、$.post、$.get)
    前端知识大全
    jquery实现2级联动
    [转]那些年我们一起清除过的浮动
    使用kubeadm在CentOS上搭建Kubernetes1.14.3集群
    企业优秀运维人员20道必会iptables面试题
    通过nginx日志利用shell统计日pv和uv
    php访问mysql接口pdo-mysql安装
    何查看已经安装的nginx、apache、mysql和php的编译参数
  • 原文地址:https://www.cnblogs.com/bangguo/p/13112285.html
Copyright © 2011-2022 走看看