Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

HttpRequest extensions

Fernando Escolar edited this page May 6, 2021 · 1 revision

These are the methods to extend the HttpRequest behavior:

Task<T> FromJsonAsync<T>(bool validateMediaType = false, CancellationToken cancellationToken = default);

T FromRoute<T>(string name);

T FromQuery<T>(string name);

bool TryFromRoute<T>(string name, out T result);

bool TryFromQuery<T>(string name, out T result);
  • FromJsonAsync: Deserializes the request body using System.Text.json.JsonSerializer. If validateMediaType is set to true it throws an exception when the media type of the request it's not "application/json". If you don't specify cancellationToken it uses the current HttpContext.RequestAborted value.
  • FromRoute: Gets a value from the request route values.
  • FromQuery: Gets a value from the request query string values.
  • TryFromRoute: Tries to get a value from the request route values. Returns false if it doesn't exists.
  • TryFromQuery: Tries to get a value from the request query string values. Returns false if it doesn't exists.
Clone this wiki locally