【问题标题】:WCF REST WebService content type is wrongWCF REST WebService 内容类型错误
【发布时间】:2011-08-04 11:21:49
【问题描述】:

我使用 WCF REST 模板 40(CS) 创建了一个简单的 REST 服务,它工作得很好。唯一的问题是响应使用“application/json”作为内容类型,但我需要“text/plain”。

该问题已在博客文章中进行了解释,但由于模板的原因,我没有使用 .svc 文件。所以建议的解决方案对我不起作用。

我的服务合同:

[ServiceContract]
public interface ICouchService
{
    [OperationContract]
    [WebInvoke(ResponseFormat = WebMessageFormat.Json, UriTemplate = "/", Method = "GET")]
    ServiceInformation Hello();

    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "/piclib/{id}")]
    CouchDocument GetDocument(string id);
}

web.config:

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>
  </system.webServer>

  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false"/>
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>

【问题讨论】:

    标签: wcf web-services rest service


    【解决方案1】:

    如果要从 WCF REST 服务返回任意内容,则需要使用原始编程模型 - http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-web.aspx。您使用的模板使用服务路由定义了端点,因此一切都为您设置好了。现在需要定义返回Stream参数的操作,并在操作中设置合适的内容类型:WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-16
      • 2015-05-01
      • 1970-01-01
      • 2011-12-24
      • 1970-01-01
      • 2016-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多