<bdo id='arRyJ'></bdo><ul id='arRyJ'></ul>
  • <small id='arRyJ'></small><noframes id='arRyJ'>

    <i id='arRyJ'><tr id='arRyJ'><dt id='arRyJ'><q id='arRyJ'><span id='arRyJ'><b id='arRyJ'><form id='arRyJ'><ins id='arRyJ'></ins><ul id='arRyJ'></ul><sub id='arRyJ'></sub></form><legend id='arRyJ'></legend><bdo id='arRyJ'><pre id='arRyJ'><center id='arRyJ'></center></pre></bdo></b><th id='arRyJ'></th></span></q></dt></tr></i><div id='arRyJ'><tfoot id='arRyJ'></tfoot><dl id='arRyJ'><fieldset id='arRyJ'></fieldset></dl></div>
  • <tfoot id='arRyJ'></tfoot>

      1. <legend id='arRyJ'><style id='arRyJ'><dir id='arRyJ'><q id='arRyJ'></q></dir></style></legend>
      2. 如何向 WCF 服务添加跨域支持

        How to add cross domain support to WCF service(如何向 WCF 服务添加跨域支持)
          <bdo id='CmDXN'></bdo><ul id='CmDXN'></ul>
          <tfoot id='CmDXN'></tfoot>
          • <i id='CmDXN'><tr id='CmDXN'><dt id='CmDXN'><q id='CmDXN'><span id='CmDXN'><b id='CmDXN'><form id='CmDXN'><ins id='CmDXN'></ins><ul id='CmDXN'></ul><sub id='CmDXN'></sub></form><legend id='CmDXN'></legend><bdo id='CmDXN'><pre id='CmDXN'><center id='CmDXN'></center></pre></bdo></b><th id='CmDXN'></th></span></q></dt></tr></i><div id='CmDXN'><tfoot id='CmDXN'></tfoot><dl id='CmDXN'><fieldset id='CmDXN'></fieldset></dl></div>

              <tbody id='CmDXN'></tbody>

            <small id='CmDXN'></small><noframes id='CmDXN'>

                  <legend id='CmDXN'><style id='CmDXN'><dir id='CmDXN'><q id='CmDXN'></q></dir></style></legend>
                  本文介绍了如何向 WCF 服务添加跨域支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试允许来自托管在 localhost:80 的 javascript 应用程序的 POST 请求到托管在不同端口的 WCF RESTful 服务,但不知何故它不起作用.我尝试将自定义属性添加到标头,并以编程方式将其添加到我的服务的 JSONData 方法中,但我的响应中仍然出现405 Method not allowed".这里的正确方法是什么?

                  I'm trying to allow POST requests from my javascript app hosted at localhost:80 to a WCF REStful service hosted at a different port, but somehow it doesn't work. I've tried adding custom properties to the header, as well as adding it programatically in my service's JSONData method but I'm still getting '405 Method not allowed' in my response. What is the proper approach here ?

                  这是我的界面:

                  namespace RestService
                  {
                      public class RestServiceImpl : IRestServiceImpl
                      {
                          #region IRestServiceImpl Members
                  
                          public string JSONData()
                          {
                              HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
                              return "Your POST request";
                          }
                  
                          #endregion
                      }
                  }
                  

                  和服务代码:

                  using System.ServiceModel;
                  using System.ServiceModel.Web;
                  using System.Web.Script.Services;
                  
                  namespace RestService
                  {
                  
                      [ServiceContract]
                      public interface IRestServiceImpl
                      {
                          [OperationContract]
                          [ScriptMethod]
                          [WebInvoke(Method = "POST",
                              ResponseFormat = WebMessageFormat.Json,
                              BodyStyle = WebMessageBodyStyle.Bare,
                              UriTemplate = "export")]
                          string JSONData();
                      }
                  }
                  

                  最后是配置:

                  <?xml version="1.0"?>
                  <configuration>
                  
                    <system.web>
                      <compilation debug="true" targetFramework="4.0" />
                    </system.web>
                    <system.serviceModel>
                      <services>
                        <service name="RestService.RestServiceImpl" behaviorConfiguration="ServiceBehaviour">
                          <endpoint address ="" binding="webHttpBinding" contract="RestService.IRestServiceImpl" behaviorConfiguration="web">
                          </endpoint>
                        </service>
                      </services>
                  
                      <behaviors>
                        <serviceBehaviors>
                          <behavior name="ServiceBehaviour">
                            <serviceMetadata httpGetEnabled="true"/>
                            <serviceDebug includeExceptionDetailInFaults="false"/>
                          </behavior>
                        </serviceBehaviors>
                        <endpointBehaviors>
                          <behavior name="web">
                            <webHttp/>
                          </behavior>
                        </endpointBehaviors>
                      </behaviors>
                      <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
                    </system.serviceModel>
                    <system.webServer>
                      <modules runAllManagedModulesForAllRequests="true"/>
                      <httpProtocol>
                        <customHeaders>
                           <add name="Access-Control-Allow-Origin" value="*" />
                        </customHeaders>
                  </httpProtocol>  
                    </system.webServer>
                  
                  </configuration>
                  

                  推荐答案

                  这对我来说比 Web.config 版本更好:

                  This worked better for me than the Web.config version:

                  创建一个Global.asax

                  将此方法添加到Global.asax.cs:

                  using System.Web;
                  
                  namespace StackOverflow
                  {
                      public class Global : System.Web.HttpApplication
                      {
                          protected void Application_BeginRequest(object sender, EventArgs e)
                          {
                              HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
                              if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
                              {
                                  HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
                                  HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
                                  HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
                                  HttpContext.Current.Response.End();
                              }
                          }
                      }
                  }
                  

                  参考:http://www.dotnet-tricks.com/Tutorial/wcf/X8QN260412-Calling-Cross-Domain-WCF-Service-using-Jquery.html

                  这篇关于如何向 WCF 服务添加跨域支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Adding and removing users from Active Directory groups in .NET(在 .NET 中的 Active Directory 组中添加和删除用户)
                  set equality in linq(在 linq 中设置相等)
                  HashSet conversion to List(HashSet 转换为 List)
                  How to set timeout for webBrowser navigate event(如何为 webBrowser 导航事件设置超时)
                  Test whether two IEnumerablelt;Tgt; have the same values with the same frequencies(测试两个IEnumerablelt;Tgt;具有相同频率的相同值)
                  How do you determine if two HashSets are equal (by value, not by reference)?(您如何确定两个 HashSet 是否相等(按值,而不是按引用)?)

                    • <tfoot id='mYngm'></tfoot>
                        <tbody id='mYngm'></tbody>

                        <bdo id='mYngm'></bdo><ul id='mYngm'></ul>

                        <legend id='mYngm'><style id='mYngm'><dir id='mYngm'><q id='mYngm'></q></dir></style></legend>

                        • <small id='mYngm'></small><noframes id='mYngm'>

                        • <i id='mYngm'><tr id='mYngm'><dt id='mYngm'><q id='mYngm'><span id='mYngm'><b id='mYngm'><form id='mYngm'><ins id='mYngm'></ins><ul id='mYngm'></ul><sub id='mYngm'></sub></form><legend id='mYngm'></legend><bdo id='mYngm'><pre id='mYngm'><center id='mYngm'></center></pre></bdo></b><th id='mYngm'></th></span></q></dt></tr></i><div id='mYngm'><tfoot id='mYngm'></tfoot><dl id='mYngm'><fieldset id='mYngm'></fieldset></dl></div>