[英]Change Response Headers on Media Type Formatter for ASP.NET Web API
我创建了一个ASP.NET Web API控制器,它在一个动作上返回一个强类型对象,如下所示:
// GET api/iosdevices/5
public iOSDevice Get(string id) {
return new iOSDevice();
}
我创建了一个BufferedMediaTypeFormatter来处理iOSDevice类型:
public class iOSDeviceXmlFormatter : BufferedMediaTypeFormatter
{
public iOSDeviceXmlFormatter() {
SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/xml"));
SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/xml"));
SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
}
public override void WriteToStream(Type type, object value, Stream writeStream, HttpContent content) {
content.Headers.ContentType = new MediaTypeHeaderValue("application/xml");
iOSDevice device = (iOSDevice)value;
using (XmlWriter writer = XmlWriter.Create(writeStream)) {
writer.WriteStartElement("iOSDevice");
if (device.identifierForVendor != Guid.Empty) {
writer.WriteElementString("identifierForVendor", device.identifierForVendor.ToString());
writer.WriteElementString("userInterfaceIdiom", device.userInterfaceIdiom);
writer.WriteElementString("systemName", device.systemName);
writer.WriteElementString("systemVersion", device.systemVersion);
writer.WriteElementString("model", device.model);
}
writer.WriteEndElement();
}
writeStream.Close();
}
}
我的问题是当我抓住“text / html”类型时(例如有人试图从他或她的网络浏览器访问API),响应类型是“text / html”而不是“application / xml”。 我想覆盖响应类型,以便用户获得“application / xml”而不是“text / html”的响应。
我不能在ApiController类型中访问常规MVC控制器上的“Response”属性,我不知所措。 如何覆盖媒体类型格式化程序正在处理的此操作的响应类型?
编辑:有用的注意事项
我之前尝试过这个:
var response = Request.CreateResponse<iOSDevice>(HttpStatusCode.Accepted, device);
response.Headers.Remove("Content-Type");
response.Headers.Add("Content-Type", "application/xml; charset=utf-8");
return response;
它声称我“滥用”标题。
但是当我使用Filip的例子直接设置内容时,它起作用了!
var response = Request.CreateResponse();
response.Content = new ObjectContent<iOSDevice>(device, new iOSDeviceXmlFormatter());
return response;
当您在格式化程序中写入流时,标题已经发送。
你可以这样做:
public HttpResponseMessage Get(string id) {
{
var value = new iOSDevice();
var response = Request.CreateResponse();
response.Content = new ObjectContent(typeof(iOSDevice), value, new iOSDeviceXmlFormatter());
//set headers on the "response"
return response;
}
或者您可以这样做(将此方法添加到您的格式化程序):
public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, string mediaType)
{
base.SetDefaultContentHeaders(type, headers, mediaType);
headers.ContentType = new MediaTypeHeaderValue("application/xml");
}
以下是我如何将SetDefaultContentHeaders
与自定义格式化程序一起使用的示例: http : //www.strathweb.com/2012/09/generate-kindle-mobi-ebooks-with-your-asp-net-web-api/
public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType)
{
if (CanWriteType(type) && mediaType.MediaType == supportedMediaType)
{
headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
headers.ContentDisposition.FileName = "ebook.mobi";
}
else
{
base.SetDefaultContentHeaders(type, headers, mediaType);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.