如何从WCP请求返回JsonP?

时间:2017-06-16 15:06:41

标签: c# .net wcf jsonp wcf-rest

我想构建一个返回JsonP请求的WCF服务。我希望它返回以下内容,

jsonpCallback({" fileNames":" IDR023.T.201705201412.png,IDR023.T.201705201418.png"});

所以我创建了以下WCF服务

IBOM.cs

  [ServiceContract]
    public interface IBOM
    {
        [OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Json)]
        string GetData();
    }

BOM.cs

 public class BOM : IBOM
    {

        public string GetData()
        {
            return "jsonpCallback({\"fileNames\":\"IDR023.T.201705201412.png, IDR023.T.201705201418.png\"});";


        }
    }

Webconfig

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="BomService.BOM" behaviorConfiguration="ServiceBehaviour">
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address ="" binding="webHttpBinding" contract="BomService.IBOM" behaviorConfiguration="web">
          <!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          -->
        </endpoint>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

但我回来了

&#34; jsonpCallback({\&#34; fileNames \&#34;:\&#34; IDR023.T.201705201412.png,IDR023.T.201705201418.png \&#34;}); &#34;

我正在返回一个字符串并尝试将其设置为返回我需要的内容。这是JSONP的最佳方式吗?还是有更好的方法来实现我的需要

由于

1 个答案:

答案 0 :(得分:0)

对于任何有兴趣的人,我都会解决这个问题。这是代码。

伊博姆

[的ServiceContract] 公共接口IBOM {

[OperationContract]
[WebInvoke(Method = "GET",
    ResponseFormat = WebMessageFormat.Json)]
Stream GetData();

}

BOM

public class BOM : IBOM
    {
      public Stream GetData()
        {


            string jsCode = "jsonpCallback" + "({\Test:\"" + fileNames +   "\"});";
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/javascript";
            return new MemoryStream(Encoding.UTF8.GetBytes(jsCode));

        }