本文介绍了如何允许CORS用于ASP.NET WebForms终结点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向Webforms样式的Web应用程序(.aspx和.asmx)添加一些带有 [WebMethod] 的终结点函数.

I am trying to add some [WebMethod] annotated endpoint functions to a Webforms style web app (.aspx and .asmx).

我想用 [EnableCors] 注释这些端点,从而获得所有好的ajax预检功能.

I'd like to annotate those endpoints with [EnableCors] and thereby get all the good ajax-preflight functionality.

VS2013接受注释,但是端点在使用CORS时效果不佳.(使用相同来源但不使用交叉来源时,它们可以很好地工作.)

VS2013 accepts the annotation, but still the endpoints don't play nice with CORS. (They work fine when used same-origin but not cross-origin).

我什至不能让它们起伏不平地起跨原点的作用

I can't even get them to function cross-origin with the down and dirty

HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Origin", "*");

方法-我的浏览器拒绝响应,并且跨域响应标头没有出现.

approach -- my browsers reject the responses, and the cross-origin response headers don't appear.

如何在这些 [WebMethod] 端点中获得CORS功能?

How can I get CORS functionality in these [WebMethod] endpoints?

推荐答案

我建议仔细检查您是否已执行此页面上的所有步骤: ASP.NET上的CORS

I recommend double-checking you have performed all steps on this page: CORS on ASP.NET

除了:

Response.AppendHeader("Access-Control-Allow-Origin", "*");

也尝试:

Response.AppendHeader("Access-Control-Allow-Methods","*");

尝试直接在Web配置中添加:

Try adding directly in web config:

<system.webServer>
   <httpProtocol>
     <customHeaders>
       <add name="Access-Control-Allow-Methods" value="*" />
       <add name="Access-Control-Allow-Headers" value="Content-Type" />
     </customHeaders>
   </httpProtocol>
</system.webServer>

如果失败,则需要确保您对两个域都拥有控制权.

Failing that, you need to ensure you have control over both domains.

这篇关于如何允许CORS用于ASP.NET WebForms终结点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 03:38