C# WCF REST 服务响应 - 删除自动生成的内容
C# WCF REST Service Response - remove autogenerated content
我创建了一个 RESTful 服务,它提供以下响应:
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">hello</string>
但是,我只希望它 return 'hello'。我该怎么做?
接口:
[ServiceContract]
public interface IApp
{
[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.WrappedRequest,
Method = "GET",
UriTemplate = "/ourapi/v1/admin/certificate")]
string retrieveInfo();
}
C#:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class App : IApp
{
public string retrieveInfo()
{
WebOperationContext ctx = WebOperationContext.Current;
var dateValue = DateTime.UtcNow.ToString("ddd, dd MMM yyyy HH:mm:ss UTC", CultureInfo.InvariantCulture);
string merchantId = "1234567";
string errorCodeValue = "00";
ctx.OutgoingResponse.ContentType = "text/plain; charset=utf-8";
ctx.OutgoingResponse.Headers.Add("date", dateValue);
ctx.OutgoingResponse.Headers.Add("merchant-id", merchantId);
ctx.OutgoingResponse.Headers.Add("error-code", errorCodeValue);
return "hello";
}
}
我一贯的做法是return一个Stream
。我还删除了您的请求和响应,并将 BodyStyle
设置为 Bare。
[WebInvoke(BodyStyle = WebMessageBodyStyle.Bare,
Method = "GET",
UriTemplate = "/ourapi/v1/admin/certificate")]
Stream retrieveInfo();
[AspNetCompatibilityRequirements(RequirementsMode =
AspNetCompatibilityRequirementsMode.Allowed)]
public class App : IApp
{
public Stream retrieveInfo()
{
WebOperationContext ctx = WebOperationContext.Current;
var dateValue = DateTime.UtcNow.ToString("ddd, dd MMM yyyy HH:mm:ss UTC", CultureInfo.InvariantCulture);
string merchantId = "1234567";
string errorCodeValue = "00";
ctx.OutgoingResponse.ContentType = "text/plain; charset=utf-8";
ctx.OutgoingResponse.Headers.Add("date", dateValue);
ctx.OutgoingResponse.Headers.Add("merchant-id", merchantId);
ctx.OutgoingResponse.Headers.Add("error-code", errorCodeValue);
return New System.IO.MemoryStream(Encoding.UTF8.GetBytes("hello"));
}
}
简单地说,你可以有两种类型的响应格式XML
和JSON
。但是你可以扩展 WCF
来产生你想要的。有关详细信息,请参阅 article
我创建了一个 RESTful 服务,它提供以下响应:
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">hello</string>
但是,我只希望它 return 'hello'。我该怎么做?
接口:
[ServiceContract]
public interface IApp
{
[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.WrappedRequest,
Method = "GET",
UriTemplate = "/ourapi/v1/admin/certificate")]
string retrieveInfo();
}
C#:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class App : IApp
{
public string retrieveInfo()
{
WebOperationContext ctx = WebOperationContext.Current;
var dateValue = DateTime.UtcNow.ToString("ddd, dd MMM yyyy HH:mm:ss UTC", CultureInfo.InvariantCulture);
string merchantId = "1234567";
string errorCodeValue = "00";
ctx.OutgoingResponse.ContentType = "text/plain; charset=utf-8";
ctx.OutgoingResponse.Headers.Add("date", dateValue);
ctx.OutgoingResponse.Headers.Add("merchant-id", merchantId);
ctx.OutgoingResponse.Headers.Add("error-code", errorCodeValue);
return "hello";
}
}
我一贯的做法是return一个Stream
。我还删除了您的请求和响应,并将 BodyStyle
设置为 Bare。
[WebInvoke(BodyStyle = WebMessageBodyStyle.Bare,
Method = "GET",
UriTemplate = "/ourapi/v1/admin/certificate")]
Stream retrieveInfo();
[AspNetCompatibilityRequirements(RequirementsMode =
AspNetCompatibilityRequirementsMode.Allowed)]
public class App : IApp
{
public Stream retrieveInfo()
{
WebOperationContext ctx = WebOperationContext.Current;
var dateValue = DateTime.UtcNow.ToString("ddd, dd MMM yyyy HH:mm:ss UTC", CultureInfo.InvariantCulture);
string merchantId = "1234567";
string errorCodeValue = "00";
ctx.OutgoingResponse.ContentType = "text/plain; charset=utf-8";
ctx.OutgoingResponse.Headers.Add("date", dateValue);
ctx.OutgoingResponse.Headers.Add("merchant-id", merchantId);
ctx.OutgoingResponse.Headers.Add("error-code", errorCodeValue);
return New System.IO.MemoryStream(Encoding.UTF8.GetBytes("hello"));
}
}
简单地说,你可以有两种类型的响应格式XML
和JSON
。但是你可以扩展 WCF
来产生你想要的。有关详细信息,请参阅 article