无法通过URL访问JSON输出

时间:2016-05-02 21:07:59

标签: json wcf rest

我在本地主机上访问URL /Resources/Feeder.svc 时得到响应的服务。但是,我无法访问 /Resources/Feeder.svc/ping ,但我使用以下代码。

[ServiceContract]
public interface IFeeder
{
  [WebInvoke(Method = "GET", 
    ResponseFormat = WebMessageFormat.Json, 
    UriTemplate = "ping")]
  [OperationContract]
  string Ping();
}

public class Feeder : IFeeder { public string Ping() { return "pung"; }

我一直尝试使用和不使用一堆属性,尝试使用和不使用 svc 等来访问服务.Nada。我画了空白。

我已尝试关注a guidetwo,但我无法看到自己错过的内容。

我得到空的命中 - 根本没有文字。该错误表示 400错误请求。什么都不告诉我......我该怎么做才能调试它?很可能它真的很蠢,因为我很累。对不起...

1 个答案:

答案 0 :(得分:1)

You haven't shown your web.config and my guess would be that you probably forgot to edit it. You need to do two things. First of all, declare an endpoint behavior. Second, add a protocol mapping. Like this.

<system.serviceModel>
  <!-- Addition of protocol mapping -->
  <protocolMapping>
    <add scheme="http" binding="webHttpBinding"/>
  </protocolMapping>
  <!-- End of addition -->
  <behaviors>
    <!-- Addition of endpoint behavior -->
    <endpointBehaviors>
      <behavior>
        <webHttp />
      </behavior >
    </endpointBehaviors>
    <!-- End of addition -->
    ...
  </behaviors>
  ...
</system.serviceModel>

Also, I don't think you actually need the attribute OperationContract if you're using WebGet. It's redundant.