WCF 4 REST响应标头 [英] WCF 4 REST response header

查看:137
本文介绍了WCF 4 REST响应标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一生中都找不到删除Get方法的自动生成部分的方法.
给出以下代码

For the life of me I can not find a way to remove the automatically generated portion of my Get method.
given following code

[WebGet(UriTemplate = "{id}", BodyStyle=WebMessageBodyStyle.Bare, ResponseFormat=WebMessageFormat.Xml)]
public string Get(string id) {
return "blah";
}



我得到以下回应.
< string xmlns ="http://schemas.microsoft.com/2003/10/Serialization/"> blah</string>

我怎样才能得到等等".
非常感谢.



I get following response.
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">blah</string>

How can I get just "blah".
Many Thanks.

推荐答案

我认为这会起作用,请注意将Stream作为返回参数.
I think this will work, note the Stream as return argument.
[WebGet(UriTemplate = "{id}", BodyStyle = WebMessageBodyStyle.Bare)]
public Stream Get(string id)
{
  OutgoingWebResponseContext context = WebOperationContext.Current.OutgoingResponse;
  context.ContentType = "text/plain";
  return new System.IO.MemoryStream(ASCIIEncoding.Default.GetBytes("blah"));
}


我将执行以下操作,而不是更改方法签名:

Instead of changing the method signature, I would do this:

HttpWebResponse restResponse = (HttpWebResponse)restRequest.GetResponse();
string value = null;

using (var st = restResponse.GetResponseStream())
{
    StreamReader sr = new StreamReader(st);
    value = sr.ReadToEnd();
}

XmlDocument doc = new XmlDocument();
doc.LoadXml(value);
value = doc.InnerText;


这篇关于WCF 4 REST响应标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆