我有一个自定义的自托管 WebApi 应用程序MediaTypeFormatter
根据“名称”参数(或 URL 的一部分),应用程序应将请求正文格式化为不同的类型。
这是动作
// http://localhost/api/fire/test/
// Route: "api/fire/{name}",
public HttpResponseMessage Post([FromUri] string name, object data)
{
// Snip
}
这是自定义 MediaTypeFormatter.ReadFromStreamAsync
public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
{
var name = "test"; // TODO this should come from the current request
var formatter = _httpSelfHostConfiguration.Formatters.JsonFormatter;
if (name.Equals("test", StringComparison.InvariantCultureIgnoreCase))
{
return formatter.ReadFromStreamAsync(typeof(SomeType), readStream, content, formatterLogger);
}
else
{
return formatter.ReadFromStreamAsync(typeof(OtherType), readStream, content, formatterLogger);
}
}