我有一个动作方法返回一个HttpResponseMessage的API,但是基于Accept头这可以返回数据的许多不同的格式。连载多个返回类型与Request.CreateResponse
这是我的那一刻,它的工作,但它不是很理想,因为我得记住,包括在MappedItem方法的任何新类,而且会有很多。
[HttpGet]
public HttpResponseMessage Get(int id)
{
var result = _builder.Build(id);
return MappedItem(result);
}
protected HttpResponseMessage MappedItem<T>(T item)
{
// Maps the class to the media type defined in the Accept header
var destinationType = GetDestinationType();
var type = typeof(T);
var mapped = Mapper.Map(item, type, destinationType);
if (mapped is ApiModelV1) {
return Request.CreateResponse(HttpStatusCode.OK, mapped as ApiModelV1);
}
return Request.CreateResponse(HttpStatusCode.OK, mapped);
}
它工作正常,没有if (mapped is ApiModelV1)
的一部分,如果我只是连载到JSON,但如果我到串行化XML抛出异常。有没有人知道以更通用的方式做到这一点?
谢谢,但如果我用KnownType属性,那么XML输出有使用上的一切d1p1命名空间。唯一可以让它输出内容的方法是使用自定义串行器,然后说'return Request.CreateResponse(HttpStatusCode.OK,映射为BaseModel);'然后根元素被命名为“BaseModel”。 – Tom