Ho un'applicazione WebAPI self-hosted con un costume MediaTypeFormatter
Get URL richiesto o un parametro di azione i MediaTypeFormatter.ReadFromStreamAsync
A seconda del parametro "nome" (o in tal modo parte dell'URL), l'applicazione deve formattare il corpo della richiesta a vari tipi.
Ecco l'azione
// http://localhost/api/fire/test/
// Route: "api/fire/{name}",
public HttpResponseMessage Post([FromUri] string name, object data)
{
// Snip
}
Ecco l'usanza 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);
}
}
grazie è stata una buona soluzione –