简体   繁体   中英

what's wrong with this wcf rest implementation?

I have a wcf rest service method. The OperationContract looks like this:

[OperationContract]
[WebGet(UriTemplate = "?maps/reporttypes/{reportType}/reportperiods/{reportPeriod}", ResponseFormat=WebMessageFormat.Json)]
Map GetData(int reportType, int reportPeriod);

I can test this successfully with the WCF Test Client so I know the underlying method works. However, I'm having problems getting the data with the $.ajax() method:

$.ajax({    
    url: 'http://localhost:52672/Service1.svc?maps/reporttypes/1/reportperiods/1',
    success:function(data){
       //bind data here
    },
    error:function(error){
    }
});

The ajax method above returns success but the data value is just the html for the page that gets displayed if you select "View in Browser" for the service. Any idea what I might be missing in this implementation?

web.config needed to be updated for rest. this article provides a good reference:

http://www.topwcftutorials.net/2013/09/simple-steps-for-restful-service.html

As you already found out there is a fundamental issue with your configuration. In order to use a restful WCF-service you need a webHttpBinding instead of the basicHttpBinding which you were using.

An example of a working config would this <system.serviceModel> -node:

<system.serviceModel>
  <services>
    <service behaviorConfiguration="Default" name="MapService5.Service1">
      <endpoint address="" behaviorConfiguration="webBehavior" binding="webHttpBinding" contract="MapService5.IService1" />
    </service>
  </services>
  <behaviors>
    <endpointBehaviors>
      <behavior name="webBehavior">
        <webHttp />
      </behavior>
    </endpointBehaviors>
    <serviceBehaviors>
      <behavior name="Default">
        <serviceMetadata httpGetEnabled="true"/>
      </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>

By having that change done, you're already able to invoke the method directly via your browser:

在此处输入图片说明

The next error reason is the already mentioned question mark which you used incorrectly. You tried to invoke the method like this:

http://localhost:52672/Service1.svc?maps/reporttypes/1/reportperiods/1

Whereas it has to be invoked like that:

http://localhost:52672/Service1.svc/maps/reporttypes/1/reportperiods/1

Please notice the slash ( / ) right after Service1.svc .

If you're using a question mark ( ? ) the rest of the string ( ?maps/reporttypes/1/reportperiods/1 ) will be treated as a query-string which is not what you're looking for in your situation. You need a simple GET without any query params, which can be achieved by using the latter link above.

The UriTemplate has to be restored to the string you had before. Again the question mark vs. slash issue ;)

[WebGet(UriTemplate = "/maps/reporttypes/{reportType}/reportperiods/{reportPeriod}", ResponseFormat=WebMessageFormat.Json)]

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