HttpRequestMessageExtensions Class¶
Provides extension methods for the System.Net.Http.HttpRequestMessage class.
- Namespace
System.Net.Http- Assemblies
- Microsoft.AspNetCore.Mvc.WebApiCompatShim
Syntax¶
public class HttpRequestMessageExtensions
-
class
System.Net.Http.HttpRequestMessageExtensions
Methods¶
-
CreateErrorResponse(System.Net.Http.HttpRequestMessage, System.Net.Http.InvalidByteRangeException)¶ Helper method for creating an
System.Net.Http.HttpResponseMessagemessage with a “416 (Requested Range Not Satisfiable)” status code. This response can be used in combination with theSystem.Net.Http.ByteRangeStreamContentto indicate that the requested range or ranges do not overlap with the current resource. The response contains a “Content-Range” header indicating the valid upper and lower bounds for requested ranges.Arguments: - request (System.Net.Http.HttpRequestMessage) – The request.
- invalidByteRangeException (System.Net.Http.InvalidByteRangeException) – An
System.Net.Http.InvalidByteRangeExceptioninstance, typically thrown by aSystem.Net.Http.ByteRangeStreamContentinstance.
Return type: System.Net.Http.HttpResponseMessage
Returns: An 416 (Requested Range Not Satisfiable) error response with a Content-Range header indicating the valid range.
public static HttpResponseMessage CreateErrorResponse(this HttpRequestMessage request, InvalidByteRangeException invalidByteRangeException)
-
CreateErrorResponse(System.Net.Http.HttpRequestMessage, System.Net.HttpStatusCode, Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary) Helper method that performs content negotiation and creates a
System.Net.Http.HttpResponseMessagerepresenting an error with an instance ofSystem.Net.Http.ObjectContent`1wrapping anSystem.Web.Http.HttpErrorfor model state <em>modelState</em>. If no formatter is found, this method returns a response with status 406 NotAcceptable.Arguments: - request (System.Net.Http.HttpRequestMessage) – The request.
- statusCode (System.Net.HttpStatusCode) – The status code of the created response.
- modelState (Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary) – The model state.
Return type: System.Net.Http.HttpResponseMessage
Returns: An error response for <em>modelState</em> with status code <em>statusCode</em>.
public static HttpResponseMessage CreateErrorResponse(this HttpRequestMessage request, HttpStatusCode statusCode, ModelStateDictionary modelState)
-
CreateErrorResponse(System.Net.Http.HttpRequestMessage, System.Net.HttpStatusCode, System.Exception) Helper method that performs content negotiation and creates a
System.Net.Http.HttpResponseMessagerepresenting an error with an instance ofSystem.Net.Http.ObjectContent`1wrapping anSystem.Web.Http.HttpErrorfor exception <em>exception</em>. If no formatter is found, this method returns a response with status 406 NotAcceptable.Arguments: - request (System.Net.Http.HttpRequestMessage) – The request.
- statusCode (System.Net.HttpStatusCode) – The status code of the created response.
- exception (System.Exception) – The exception.
Return type: System.Net.Http.HttpResponseMessage
Returns: An error response for <em>exception</em> with status code <em>statusCode</em>.
public static HttpResponseMessage CreateErrorResponse(this HttpRequestMessage request, HttpStatusCode statusCode, Exception exception)
-
CreateErrorResponse(System.Net.Http.HttpRequestMessage, System.Net.HttpStatusCode, System.String) Helper method that performs content negotiation and creates a
System.Net.Http.HttpResponseMessagerepresenting an error with an instance ofSystem.Net.Http.ObjectContent`1wrapping anSystem.Web.Http.HttpErrorwith message <em>message</em>. If no formatter is found, this method returns a response with status 406 NotAcceptable.Arguments: - request (System.Net.Http.HttpRequestMessage) – The request.
- statusCode (System.Net.HttpStatusCode) – The status code of the created response.
- message (System.String) – The error message.
Return type: System.Net.Http.HttpResponseMessage
Returns: An error response with error message <em>message</em> and status code <em>statusCode</em>.
public static HttpResponseMessage CreateErrorResponse(this HttpRequestMessage request, HttpStatusCode statusCode, string message)
-
CreateErrorResponse(System.Net.Http.HttpRequestMessage, System.Net.HttpStatusCode, System.String, System.Exception) Helper method that performs content negotiation and creates a
System.Net.Http.HttpResponseMessagerepresenting an error with an instance ofSystem.Net.Http.ObjectContent`1wrapping anSystem.Web.Http.HttpErrorwith error message <em>message</em> for exception <em>exception</em>. If no formatter is found, this method returns a response with status 406 NotAcceptable.Arguments: - request (System.Net.Http.HttpRequestMessage) – The request.
- statusCode (System.Net.HttpStatusCode) – The status code of the created response.
- message (System.String) – The error message.
- exception (System.Exception) – The exception.
Return type: System.Net.Http.HttpResponseMessage
Returns: An error response for <em>exception</em> with error message <em>message</em> and status code <em>statusCode</em>.
public static HttpResponseMessage CreateErrorResponse(this HttpRequestMessage request, HttpStatusCode statusCode, string message, Exception exception)
-
CreateErrorResponse(System.Net.Http.HttpRequestMessage, System.Net.HttpStatusCode, System.Web.Http.HttpError) Helper method that performs content negotiation and creates a
System.Net.Http.HttpResponseMessagerepresenting an error with an instance ofSystem.Net.Http.ObjectContent`1wrapping <em>error</em> as the content. If no formatter is found, this method returns a response with status 406 NotAcceptable.Arguments: - request (System.Net.Http.HttpRequestMessage) – The request.
- statusCode (System.Net.HttpStatusCode) – The status code of the created response.
- error (System.Web.Http.HttpError) – The error to wrap.
Return type: System.Net.Http.HttpResponseMessage
Returns: An error response wrapping <em>error</em> with status code <em>statusCode</em>.
public static HttpResponseMessage CreateErrorResponse(this HttpRequestMessage request, HttpStatusCode statusCode, HttpError error)
-
CreateResponse<T>(System.Net.Http.HttpRequestMessage, System.Net.HttpStatusCode, T)¶ Helper method that performs content negotiation and creates a
System.Net.Http.HttpResponseMessagewith an instance ofSystem.Net.Http.ObjectContent`1as the content if a formatter can be found. If no formatter is found, this method returns a response with status 406 NotAcceptable. configuration.Arguments: - request (System.Net.Http.HttpRequestMessage) – The request.
- statusCode (System.Net.HttpStatusCode) – The status code of the created response.
- value (T) – The value to wrap. Can be <code>null</code>.
Return type: System.Net.Http.HttpResponseMessage
Returns: A response wrapping <em>value</em> with <em>statusCode</em>.
public static HttpResponseMessage CreateResponse<T>(this HttpRequestMessage request, HttpStatusCode statusCode, T value)
-
CreateResponse<T>(System.Net.Http.HttpRequestMessage, System.Net.HttpStatusCode, T, System.Collections.Generic.IEnumerable<System.Net.Http.Formatting.MediaTypeFormatter>) Helper method that performs content negotiation and creates a
System.Net.Http.HttpResponseMessagewith an instance ofSystem.Net.Http.ObjectContent`1as the content if a formatter can be found. If no formatter is found, this method returns a response with status 406 NotAcceptable.Arguments: - request (System.Net.Http.HttpRequestMessage) – The request.
- statusCode (System.Net.HttpStatusCode) – The status code of the created response.
- value (T) – The value to wrap. Can be <code>null</code>.
- formatters (System.Collections.Generic.IEnumerable<System.Net.Http.Formatting.MediaTypeFormatter>) – The set of
System.Net.Http.Formatting.MediaTypeFormatterobjects from which to choose.
Return type: System.Net.Http.HttpResponseMessage
Returns: A response wrapping <em>value</em> with <em>statusCode</em>.
public static HttpResponseMessage CreateResponse<T>(this HttpRequestMessage request, HttpStatusCode statusCode, T value, IEnumerable<MediaTypeFormatter> formatters)
-
CreateResponse<T>(System.Net.Http.HttpRequestMessage, System.Net.HttpStatusCode, T, System.Net.Http.Formatting.MediaTypeFormatter) Helper method that creates a
System.Net.Http.HttpResponseMessagewith anSystem.Net.Http.ObjectContent`1instance containing the provided <em>value</em> and the given <em>formatter</em>.Arguments: - request (System.Net.Http.HttpRequestMessage) – The request.
- statusCode (System.Net.HttpStatusCode) – The status code of the created response.
- value (T) – The value to wrap. Can be <code>null</code>.
- formatter (System.Net.Http.Formatting.MediaTypeFormatter) – The formatter to use.
Return type: System.Net.Http.HttpResponseMessage
Returns: A response wrapping <em>value</em> with <em>statusCode</em>.
public static HttpResponseMessage CreateResponse<T>(this HttpRequestMessage request, HttpStatusCode statusCode, T value, MediaTypeFormatter formatter)
-
CreateResponse<T>(System.Net.Http.HttpRequestMessage, System.Net.HttpStatusCode, T, System.Net.Http.Formatting.MediaTypeFormatter, System.Net.Http.Headers.MediaTypeHeaderValue) Helper method that creates a
System.Net.Http.HttpResponseMessagewith anSystem.Net.Http.ObjectContent`1instance containing the provided <em>value</em> and the given <em>formatter</em>.Arguments: - request (System.Net.Http.HttpRequestMessage) – The request.
- statusCode (System.Net.HttpStatusCode) – The status code of the created response.
- value (T) – The value to wrap. Can be <code>null</code>.
- formatter (System.Net.Http.Formatting.MediaTypeFormatter) – The formatter to use.
- mediaType (System.Net.Http.Headers.MediaTypeHeaderValue) – The media type override to set on the response’s content. Can be <code>null</code>.
Return type: System.Net.Http.HttpResponseMessage
Returns: A response wrapping <em>value</em> with <em>statusCode</em>.
public static HttpResponseMessage CreateResponse<T>(this HttpRequestMessage request, HttpStatusCode statusCode, T value, MediaTypeFormatter formatter, MediaTypeHeaderValue mediaType)
-
CreateResponse<T>(System.Net.Http.HttpRequestMessage, System.Net.HttpStatusCode, T, System.Net.Http.Formatting.MediaTypeFormatter, System.String) Helper method that creates a
System.Net.Http.HttpResponseMessagewith anSystem.Net.Http.ObjectContent`1instance containing the provided <em>value</em> and the given <em>formatter</em>.Arguments: - request (System.Net.Http.HttpRequestMessage) – The request.
- statusCode (System.Net.HttpStatusCode) – The status code of the created response.
- value (T) – The value to wrap. Can be <code>null</code>.
- formatter (System.Net.Http.Formatting.MediaTypeFormatter) – The formatter to use.
- mediaType (System.String) – The media type override to set on the response’s content. Can be <code>null</code>.
Return type: System.Net.Http.HttpResponseMessage
Returns: A response wrapping <em>value</em> with <em>statusCode</em>.
public static HttpResponseMessage CreateResponse<T>(this HttpRequestMessage request, HttpStatusCode statusCode, T value, MediaTypeFormatter formatter, string mediaType)
-
CreateResponse<T>(System.Net.Http.HttpRequestMessage, System.Net.HttpStatusCode, T, System.Net.Http.Headers.MediaTypeHeaderValue) Helper method that creates a
System.Net.Http.HttpResponseMessagewith anSystem.Net.Http.ObjectContent`1instance containing the provided <em>value</em>. The given <em>mediaType</em> is used to find an instance ofSystem.Net.Http.Formatting.MediaTypeFormatter.Arguments: - request (System.Net.Http.HttpRequestMessage) – The request.
- statusCode (System.Net.HttpStatusCode) – The status code of the created response.
- value (T) – The value to wrap. Can be <code>null</code>.
- mediaType (System.Net.Http.Headers.MediaTypeHeaderValue) – The media type used to look up an instance of
System.Net.Http.Formatting.MediaTypeFormatter.
Return type: System.Net.Http.HttpResponseMessage
Returns: A response wrapping <em>value</em> with <em>statusCode</em>.
public static HttpResponseMessage CreateResponse<T>(this HttpRequestMessage request, HttpStatusCode statusCode, T value, MediaTypeHeaderValue mediaType)
-
CreateResponse<T>(System.Net.Http.HttpRequestMessage, System.Net.HttpStatusCode, T, System.String) Helper method that creates a
System.Net.Http.HttpResponseMessagewith anSystem.Net.Http.ObjectContent`1instance containing the provided <em>value</em>. The given <em>mediaType</em> is used to find an instance ofSystem.Net.Http.Formatting.MediaTypeFormatter.Arguments: - request (System.Net.Http.HttpRequestMessage) – The request.
- statusCode (System.Net.HttpStatusCode) – The status code of the created response.
- value (T) – The value to wrap. Can be <code>null</code>.
- mediaType (System.String) – The media type used to look up an instance of
System.Net.Http.Formatting.MediaTypeFormatter.
Return type: System.Net.Http.HttpResponseMessage
Returns: A response wrapping <em>value</em> with <em>statusCode</em>.
public static HttpResponseMessage CreateResponse<T>(this HttpRequestMessage request, HttpStatusCode statusCode, T value, string mediaType)
-
CreateResponse<T>(System.Net.Http.HttpRequestMessage, T) Helper method that performs content negotiation and creates a
System.Net.Http.HttpResponseMessagewith an instance ofSystem.Net.Http.ObjectContent`1as the content andSystem.Net.HttpStatusCode.OKas the status code if a formatter can be found. If no formatter is found, this method returns a response with status 406 NotAcceptable.Arguments: - request (System.Net.Http.HttpRequestMessage) – The request.
- value (T) – The value to wrap. Can be <code>null</code>.
Return type: System.Net.Http.HttpResponseMessage
Returns: A response wrapping <em>value</em> with
System.Net.HttpStatusCode.OKstatus code.public static HttpResponseMessage CreateResponse<T>(this HttpRequestMessage request, T value)
-