gpt4 book ai didi

jquery - CORS 启用 WCF REST 和 Kendo

转载 作者:行者123 更新时间:2023-12-01 04:10:06 25 4
gpt4 key购买 nike

我正在 wcf 休息服务工作。我需要允许 cors 支持跨域访问。我阅读了文章并得到了两种方法。

第一种方法是在 global.asax 的 application_beginrequest 事件中添加 http header 。这对我来说效果很好。我使用 jquery 使用剑道图表绑定(bind)对此进行了测试。图表在 IE、Chrome 和 Firefox 中填充。在global.asax中启用cors的工作代码是

    protected void Application_BeginRequest(object sender, EventArgs e)
{

HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
//These headers are handling the "pre-flight" OPTIONS call sent by the browser
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Authorization, Origin, Content-Type, Accept, X-Requested-With");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}

但是我需要配置启用cors的属性,所以我遵循了这个link 。我复制并运行该服务,结果成功。但当我在端点中启用此行为后,客户端没有在 Chrome 和 Firefox 中显示图表。所以没有启用跨域。我对吗?我想念这里。

我的新服务类别是;

public class CorsEnabledBehavior : BehaviorExtensionElement, IEndpointBehavior
{
public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{

}

public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
{

}

public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
{
var requiredHeaders = new Dictionary<string, string>();

requiredHeaders.Add("Access-Control-Allow-Origin", "*");
requiredHeaders.Add("Access-Control-Request-Method", "POST,GET,PUT,DELETE,OPTIONS");
requiredHeaders.Add("Access-Control-Allow-Headers", "X-Requested-With,Content-Type");

endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new CorsEnabledMessageInspector(requiredHeaders));
}

public void Validate(ServiceEndpoint endpoint)
{

}

public override Type BehaviorType
{
get { return typeof(CorsEnabledBehavior); }
}

protected override object CreateBehavior()
{
return new CorsEnabledBehavior();
}

}

public class CorsEnabledMessageInspector : IDispatchMessageInspector
{
Dictionary<string, string> requiredHeaders;
public CorsEnabledMessageInspector(Dictionary<string, string> headers)
{
requiredHeaders = headers ?? new Dictionary<string, string>();
}

public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
{
return null;
}

public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
var httpHeader = reply.Properties["httpResponse"] as HttpResponseMessageProperty;
foreach (var item in requiredHeaders)
{
httpHeader.Headers.Add(item.Key, item.Value);
}
}
}

我的网络配置是

<extensions>
<behaviorExtensions>
<add name="corsEnabledBehaviour" type="LAMI.Service.Utilities.CorsEnabledBehavior, LAMI.Service.Utilities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
</extensions>

<behaviors>
<endpointBehaviors>
<behavior name="endBehaviour1">
<webHttp helpEnabled="true" />
<corsEnabledBehaviour />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="serviceBehaviour1">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="webHttpConfiguration" >
</binding>
</webHttpBinding>
</bindings>

<services>
<service behaviorConfiguration="serviceBehaviour1" name="LAMI.Service.Service1">
<endpoint address="" behaviorConfiguration="endBehaviour1" binding="webHttpBinding"
bindingConfiguration="webHttpConfiguration" contract="LAMI.Service.Contract.IService1" />
<host>
<baseAddresses>
<add baseAddress="http://ltms0/ServiceApp/Service1/" />
</baseAddresses>
</host>
</service>
</services>

你能帮我解决我错过的地方吗?

最佳答案

我完成了这个。我使用 global.asax 启用了 cors 并成功工作。问题是 iecors.js。这无法在 IE 8 和 9 中启用 cors。所有其他浏览器都工作正常

关于jquery - CORS 启用 WCF REST 和 Kendo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21602415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com