PhoneGap中文网

标题: .net 后端支持跨域请求的指令 [打印本页]

作者: bdqn_wanghaoyu    时间: 2016-1-4 16:48
标题: .net 后端支持跨域请求的指令
PHP中使用header('Access-Control-Allow-Origin: *'); 允许跨域请求数据,那.net中应该添加什么指令?

作者: ionicwang    时间: 2016-1-4 16:53
http://www.cnblogs.com/IProgrami ... p_Post_Request.html


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

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

作者: bdqn_wanghaoyu    时间: 2016-1-5 13:23
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
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
之前很长时间就解决了这个问题 ,一直没有来的级整理,上边的在。net中新建一个类AllowOriginAttribute,然后再需要使用的页面添加  [Common.ControllerAllowOrigin(AllowSites = new string[] { "*" })]就阔仪了,这是同事帮我i解决的,再此感谢




欢迎光临 PhoneGap中文网 (http://bbs.phonegap100.com/) Powered by Discuz! X3.2