Daily Weekly Monthly

Daily Shaarli

All links of one day in a single page.

October 22, 2018

Accepting Raw Request Body Content in ASP.NET Core API Controllers - Rick Strahl's Web Log

A few years back I wrote a post about Accepting Raw Request Content with ASP.NET Web API. Unfortunately the process to get at raw request data is rather indirect, with no direct way to receive raw data into Controller action parameters and that hasn't really changed in ASP.NET Core's MVC/API implementation. The way the Conneg algorithm works in regards to generic data formats is roughly the same as it was with Web API.

The good news is that it's quite a bit easier to create custom formatters in ASP.NET Core that let you customize how to handle 'unknown' content types in your controllers.

Let's take a look.

ASP.NET Core 2.0: Implementing a 3rd Party Web API - DEV Community 👩‍💻👨‍💻

public class GetRandomGif : IGetRandomGif
{
public async Task ReturnRandomGifBasedOnTag(string searchCritera)
{
const string giphyKey = "";
using (var client = new HttpClient())
{
var url = new Uri($"http://api.giphy.com/v1/gifs/search?api_key={giphyKey}&q={searchCritera}&limit=1");
var response = await client.GetAsync(url);
string json;
using (var content = response.Content)
{
json = await content.ReadAsStringAsync();
}
return JsonConvert.DeserializeObject(json);
}
}
}