PhoneGap中文网

 找回密码
 立即注册
查看: 19167|回复: 4
打印 上一主题 下一主题

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

[复制链接]

3

主题

11

帖子

157

积分

注册会员

Rank: 2

积分
157
跳转到指定楼层
楼主
发表于 2016-1-4 16:48:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
PHP中使用header('Access-Control-Allow-Origin: *'); 允许跨域请求数据,那.net中应该添加什么指令?
回复

使用道具 举报

11

主题

529

帖子

1418

积分

金牌会员

Rank: 6Rank: 6

积分
1418
推荐
发表于 2016-1-4 16:53:45 | 只看该作者
回复 支持 1 反对 0

使用道具 举报

3

主题

11

帖子

157

积分

注册会员

Rank: 2

积分
157
板凳
 楼主| 发表于 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

谢谢,正在学习,感觉不错
回复 支持 反对

使用道具 举报

3

主题

11

帖子

157

积分

注册会员

Rank: 2

积分
157
地板
 楼主| 发表于 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);
        }

    }


}
回复 支持 反对

使用道具 举报

3

主题

11

帖子

157

积分

注册会员

Rank: 2

积分
157
5#
 楼主| 发表于 2016-1-18 10:05:52 | 只看该作者
之前很长时间就解决了这个问题 ,一直没有来的级整理,上边的在。net中新建一个类AllowOriginAttribute,然后再需要使用的页面添加  [Common.ControllerAllowOrigin(AllowSites = new string[] { "*" })]就阔仪了,这是同事帮我i解决的,再此感谢
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐 上一条 /1 下一条

ionic4视频教程

Archiver|手机版|小黑屋| PhoneGap中文网 ( 京ICP备13027796号-1 )  

GMT+8, 2024-4-19 13:38 , Processed in 0.036609 second(s), 27 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表