bdqn_wanghaoyu 发表于 2016-1-4 16:48:56

.net 后端支持跨域请求的指令

PHP中使用header('Access-Control-Allow-Origin: *'); 允许跨域请求数据,那.net中应该添加什么指令?

ionicwang 发表于 2016-1-4 16:53:45

http://www.cnblogs.com/IPrograming/archive/2012/05/16/CSharp_Post_Request.html


http://blog.csdn.net/ysy441088327/article/details/6384448

这两个看看看看可以不 试试

bdqn_wanghaoyu 发表于 2016-1-5 13:23:58

ionicwang 发表于 2016-1-4 16:53
http://www.cnblogs.com/IPrograming/archive/2012/05/16/CSharp_Post_Request.html




谢谢,正在学习,感觉不错

bdqn_wanghaoyu 发表于 2016-1-18 10:00:09

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace mjcApp_Service.Common
{
    public class AllowOriginAttribute
    {
      public static void onExcute(ControllerContext context, string[] AllowSites)
      {
            var origin = context.HttpContext.Request.Headers["Origin"];
            Action action = () =>
            {
                context.HttpContext.Response.AppendHeader("Access-Control-Allow-Origin", "*");

            };
            if (AllowSites != null && AllowSites.Any())
            {
                if (AllowSites.Contains(origin))
                {
                  action();
                }
            }


      }
    }
    public class ActionAllowOriginAttribute : ActionFilterAttribute
    {
      public string[] AllowSites { get; set; }
      public override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext)
      {
            AllowOriginAttribute.onExcute(filterContext, AllowSites);
            base.OnActionExecuting(filterContext);
      }
    }
    public class ControllerAllowOriginAttribute : AuthorizeAttribute
    {
      public string[] AllowSites { get; set; }
      public override void OnAuthorization(System.Web.Mvc.AuthorizationContext filterContext)
      {
            AllowOriginAttribute.onExcute(filterContext, AllowSites);
      }

    }


}

bdqn_wanghaoyu 发表于 2016-1-18 10:05:52

之前很长时间就解决了这个问题 ,一直没有来的级整理,上边的在。net中新建一个类AllowOriginAttribute,然后再需要使用的页面添加 { "*" })]就阔仪了,这是同事帮我i解决的,再此感谢
页: [1]
查看完整版本: .net 后端支持跨域请求的指令