カスタムの自己ホスト WebApi アプリケーションがあります。MediaTypeFormatter
「name」パラメーター (または 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);
}
}