【问题标题】:RESTful C# service not responding in jsonRESTful C# 服务在 json 中没有响应
【发布时间】:2018-10-24 16:28:28
【问题描述】:

我有一个 RESTful 服务,它已经存在了一段时间,只要调用它的方法就会返回 json。就在最近我添加了一个新方法,编写方式与其他方法完全相同,但它只在 xml 中响应。

以下是现有的方法接口定义:

[ServiceContract]
public interface IAccess
{

    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "GetRunList?username={username}&FromRun={FromRun}&ToRun={ToRun}&Project={Project}")]
    List<RunInfo> GetRunList(string username, int FromRun, int ToRun, string Project = null);

    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "GetPressureTap?RunNumber={RunNumber}")]
    ServicePressureTapMap GetPressureTap(int RunNumber);

    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "GetPressureTapByAttitude?RunNumber={RunNumber}&FRH={FRH}&RRH={RRH}&Yaw={Yaw}&Steer={Steer}&Roll={Roll}&Exhaust={Exhaust}")]
    List<ServiceAttitudePressureTap> GetPressureTapByAttitude(int RunNumber, decimal FRH, decimal RRH, decimal Yaw, decimal Steer, decimal Roll, decimal Exhaust);

    [OperationContract]
    [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "GetPresTapImage?RunNumber={RunNumber}&MapGuid={MapGuid}&MapName={MapName}&Version={Version}")]
    Stream GetPresTapImage(int RunNumber, string MapGuid, string MapName, int Version);

}

我所做的只是在底部添加了以下新方法:

    [OperationContract]
    [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "GetRunConfigs?RunNumber={RunNumber}")]
    List<ServiceRunConfig> GetRunConfigs(int RunNumber);

返回的类都以相同的方式声明。只有新方法以 XML 形式返回。我错过了什么明显的东西吗?

【问题讨论】:

  • 您是否使用正确的 Accept 标头调用 API?
  • 是的,它设置为 json。

标签: c# rest web-services


【解决方案1】:

事实证明,经过大量搜索,我需要添加以下内容:

<endpointBehaviors>
  <behavior name="endpointBehavior">
    <enableWebScript />
    <webHttp defaultBodyStyle="Wrapped" defaultOutgoingResponseFormat="Json" />
  </behavior>
</endpointBehaviors>

到我的 web.config,尽管在接口中有 WebInvoke 行,它确实做同样的事情。去图...

【讨论】:

    猜你喜欢
    • 2015-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多