<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 服务添加跨域支持

        时间:2023-10-25
          <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 服务添加跨域支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:获取 Request.Headers 值 下一篇:在 c# 中请求网页欺骗主机

                  相关文章

                  1. <tfoot id='mYngm'></tfoot>

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

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

                  2. <small id='mYngm'></small><noframes id='mYngm'>

                  3. <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>