简体   繁体   中英

WCF REST service does not work with parameter in URI

I have a WCF REST service (.NET4).

It works fine (browser, ajax call) for calls without parameters but I cannot get it to work with parameters. Neither in the browser nor via an ajax call.

My contract:

[OperationContract]
[WebGet(
    ResponseFormat = WebMessageFormat.Json,
    UriTemplate = "knowledgefields")]
IEnumerable<cKnowledgeField> GetKnowledgeFields();

[OperationContract]
[WebGet(
    BodyStyle = WebMessageBodyStyle.Bare,
    ResponseFormat = WebMessageFormat.Json,
    RequestFormat = WebMessageFormat.Json,
    UriTemplate = "knowledgeitems?id={id}")]
IEnumerable<cKnowledgeItem> GetKnowledgeItemsByField(string id);

My web.config

<configuration>
  <appSettings/>
  <system.web>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
      </assemblies>
    </compilation>
  </system.web>
  <system.serviceModel>
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint name="" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat="Json"/>
      </webHttpEndpoint>
    </standardEndpoints>
    <services>
      <service name="ExpertData.expertREST" behaviorConfiguration="META">
        <endpoint address="" bindingConfiguration="webHttpBindingWithJsonP" binding="webHttpBinding" contract="ExpertData.IexpertREST"/>
      </service>
    </services>
    <protocolMapping>
      <add scheme="http" binding="webHttpBinding"/>
    </protocolMapping>
    <behaviors>
      <serviceBehaviors>
        <behavior name="META">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior>
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <bindings>
      <webHttpBinding>
        <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true"/>
      </webHttpBinding>
    </bindings>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="false" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>
  <connectionStrings>
    <add name="FindAnExpertEntities" connectionString="metadata=res://*/ExpertData.csdl|res://*/ExpertData.ssdl|res://*/ExpertData.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=WIN8ATWORK\SQLEXPRESS;initial catalog=FindAnExpert;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient"/>
  </connectionStrings>
</configuration>

Ajax call:

  function getKnowledgeItems(id) {
      return $.ajax({
          url: "http://localhost:31634/expertREST.svc/knowledgeitems",
          dataType: "jsonp"
          data: { "id" : id + "" }
      }).then( function( data, textStatus, jqXHR ) {
          amplify.publish( "knowledgeItemsdata.updated", data );
      });
  }

Your AJAX call is passing the ID in the message body, BUT you service expects it as as part of the query string. Change javascript to:

  return $.ajax({
      url: "http://localhost:31634/expertREST.svc/knowledgeitems"
           + "?id=" + id,
      dataType: "jsonp"
  }).then( function( data, textStatus, jqXHR ) {
      amplify.publish( "knowledgeItemsdata.updated", data );
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM