Table of Contents

Class ApiController

Namespace
System.Web.Http
Assembly
System.Web.Http.dll

Defines properties and methods for API controller.

public abstract class ApiController : IHttpController, IDisposable
Inheritance
ApiController
Implements
Inherited Members

Constructors

ApiController()

protected ApiController()

Properties

ActionContext

Gets the action context.

public HttpActionContext ActionContext { get; set; }

Property Value

HttpActionContext

The action context.

Configuration

Gets the HttpConfiguration of the current ApiController.

public HttpConfiguration Configuration { get; set; }

Property Value

HttpConfiguration

The HttpConfiguration of the current ApiController.

ControllerContext

Gets the HttpConfiguration of the current ApiController.

public HttpControllerContext ControllerContext { get; set; }

Property Value

HttpControllerContext

The HttpConfiguration of the current ApiController.

ModelState

Gets the model state after the model binding process.

public ModelStateDictionary ModelState { get; }

Property Value

ModelStateDictionary

The model state after the model binding process.

Request

Gets or sets the HttpRequestMessage of the current ApiController.

public HttpRequestMessage Request { get; set; }

Property Value

HttpRequestMessage

The HttpRequestMessage of the current ApiController.

RequestContext

Gets the request context.

public HttpRequestContext RequestContext { get; set; }

Property Value

HttpRequestContext

The request context.

Url

Gets an instance of a UrlHelper, which is used to generate URLs to other APIs.

public UrlHelper Url { get; set; }

Property Value

UrlHelper

A UrlHelper, which is used to generate URLs to other APIs.

User

Returns the current principal associated with this request.

public IPrincipal User { get; set; }

Property Value

IPrincipal

The current principal associated with this request.

Methods

BadRequest()

Creates a BadRequestResult.

protected virtual BadRequestResult BadRequest()

Returns

BadRequestResult

A BadRequestResult.

BadRequest(string)

Creates an System.Web.Http.Results.ErrorMessageResult (400 Bad Request) with the specified error message.

protected virtual BadRequestErrorMessageResult BadRequest(string message)

Parameters

message string

The user-visible error message.

Returns

BadRequestErrorMessageResult

An InvalidModelStateResult with the specified model state.

BadRequest(ModelStateDictionary)

Creates an InvalidModelStateResult with the specified model state.

protected virtual InvalidModelStateResult BadRequest(ModelStateDictionary modelState)

Parameters

modelState ModelStateDictionary

The model state to include in the error.

Returns

InvalidModelStateResult

An InvalidModelStateResult with the specified model state.

Conflict()

Creates a ConflictResult (409 Conflict).

protected virtual ConflictResult Conflict()

Returns

ConflictResult

A ConflictResult.

Content<T>(HttpStatusCode, T)

Creates a <see cref="T:System.Web.Http.NegotiatedContentResult`1" /> with the specified values.

protected virtual NegotiatedContentResult<T> Content<T>(HttpStatusCode statusCode, T value)

Parameters

statusCode HttpStatusCode

The HTTP status code for the response message.

value T

The content value to negotiate and format in the entity body.

Returns

NegotiatedContentResult<T>

A <see cref="T:System.Web.Http.NegotiatedContentResult`1" /> with the specified values.

Type Parameters

T

The type of content in the entity body.

Content<T>(HttpStatusCode, T, MediaTypeFormatter)

Creates a <see cref="T:System.Web.Http.FormattedContentResult`1" /> with the specified values.

protected FormattedContentResult<T> Content<T>(HttpStatusCode statusCode, T value, MediaTypeFormatter formatter)

Parameters

statusCode HttpStatusCode

The HTTP status code for the response message.

value T

The content value to format in the entity body.

formatter MediaTypeFormatter

The formatter to use to format the content.

Returns

FormattedContentResult<T>

A <see cref="T:System.Web.Http.FormattedContentResult`1" /> with the specified values.

Type Parameters

T

The type of content in the entity body.

Content<T>(HttpStatusCode, T, MediaTypeFormatter, MediaTypeHeaderValue)

Creates a <see cref="T:System.Web.Http.FormattedContentResult`1" /> with the specified values.

protected virtual FormattedContentResult<T> Content<T>(HttpStatusCode statusCode, T value, MediaTypeFormatter formatter, MediaTypeHeaderValue mediaType)

Parameters

statusCode HttpStatusCode

The HTTP status code for the response message.

value T

The content value to format in the entity body.

formatter MediaTypeFormatter

The formatter to use to format the content.

mediaType MediaTypeHeaderValue

The value for the Content-Type header, or <see langword="null" /> to have the formatter pick a default value.

Returns

FormattedContentResult<T>

A <see cref="T:System.Web.Http.FormattedContentResult`1" /> with the specified values.

Type Parameters

T

The type of content in the entity body.

Content<T>(HttpStatusCode, T, MediaTypeFormatter, string)

Creates a <see cref="T:System.Web.Http.FormattedContentResult`1" /> with the specified values.

protected FormattedContentResult<T> Content<T>(HttpStatusCode statusCode, T value, MediaTypeFormatter formatter, string mediaType)

Parameters

statusCode HttpStatusCode

The HTTP status code for the response message.

value T

The content value to format in the entity body.

formatter MediaTypeFormatter

The formatter to use to format the content.

mediaType string

The value for the Content-Type header.

Returns

FormattedContentResult<T>

A <see cref="T:System.Web.Http.FormattedContentResult`1" /> with the specified values.

Type Parameters

T

The type of content in the entity body.

CreatedAtRoute<T>(string, IDictionary<string, object>, T)

Creates a CreatedAtRouteNegotiatedContentResult<T> (201 Created) with the specified values.

protected virtual CreatedAtRouteNegotiatedContentResult<T> CreatedAtRoute<T>(string routeName, IDictionary<string, object> routeValues, T content)

Parameters

routeName string

The name of the route to use for generating the URL.

routeValues IDictionary<string, object>

The route data to use for generating the URL.

content T

The content value to negotiate and format in the entity body.

Returns

CreatedAtRouteNegotiatedContentResult<T>

A CreatedAtRouteNegotiatedContentResult<T> with the specified values.

Type Parameters

T

The type of content in the entity body.

CreatedAtRoute<T>(string, object, T)

Creates a CreatedAtRouteNegotiatedContentResult<T> (201 Created) with the specified values.

protected CreatedAtRouteNegotiatedContentResult<T> CreatedAtRoute<T>(string routeName, object routeValues, T content)

Parameters

routeName string

The name of the route to use for generating the URL.

routeValues object

The route data to use for generating the URL.

content T

The content value to negotiate and format in the entity body.

Returns

CreatedAtRouteNegotiatedContentResult<T>

A CreatedAtRouteNegotiatedContentResult<T> with the specified values.

Type Parameters

T

The type of content in the entity body.

Created<T>(string, T)

Creates a CreatedNegotiatedContentResult<T> (201 Created) with the specified values.

protected CreatedNegotiatedContentResult<T> Created<T>(string location, T content)

Parameters

location string

The location at which the content has been created.

content T

The content value to negotiate and format in the entity body.

Returns

CreatedNegotiatedContentResult<T>

A CreatedNegotiatedContentResult<T> with the specified values.

Type Parameters

T

The type of content in the entity body.

Created<T>(Uri, T)

Creates a CreatedNegotiatedContentResult<T> (201 Created) with the specified values.

protected virtual CreatedNegotiatedContentResult<T> Created<T>(Uri location, T content)

Parameters

location Uri

The location at which the content has been created.

content T

The content value to negotiate and format in the entity body.

Returns

CreatedNegotiatedContentResult<T>

A CreatedNegotiatedContentResult<T> with the specified values.

Type Parameters

T

The type of content in the entity body.

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

Dispose(bool)

Releases the unmanaged resources that are used by the object and, optionally, releases the managed resources.

protected virtual void Dispose(bool disposing)

Parameters

disposing bool

true to release both managed and unmanaged resources; false to release only unmanaged resources.

ExecuteAsync(HttpControllerContext, CancellationToken)

Executes asynchronously a single HTTP operation.

public virtual Task<HttpResponseMessage> ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)

Parameters

controllerContext HttpControllerContext

The controller context for a single HTTP operation.

cancellationToken CancellationToken

The cancellation token assigned for the HTTP operation.

Returns

Task<HttpResponseMessage>

The newly started task.

Initialize(HttpControllerContext)

Initializes the ApiController instance with the specified controllerContext.

protected virtual void Initialize(HttpControllerContext controllerContext)

Parameters

controllerContext HttpControllerContext

The HttpControllerContext object that is used for the initialization.

InternalServerError()

Creates an InternalServerErrorResult (500 Internal Server Error).

protected virtual InternalServerErrorResult InternalServerError()

Returns

InternalServerErrorResult

A InternalServerErrorResult.

InternalServerError(Exception)

Creates an ExceptionResult (500 Internal Server Error) with the specified exception.

protected virtual ExceptionResult InternalServerError(Exception exception)

Parameters

exception Exception

The exception to include in the error.

Returns

ExceptionResult

An ExceptionResult with the specified exception.

Json<T>(T)

Creates a JsonResult<T> (200 OK) with the specified value.

protected JsonResult<T> Json<T>(T content)

Parameters

content T

The content value to serialize in the entity body.

Returns

JsonResult<T>

A JsonResult<T> with the specified value.

Type Parameters

T

The type of content in the entity body.

Json<T>(T, JsonSerializerSettings)

Creates a JsonResult<T> (200 OK) with the specified values.

protected JsonResult<T> Json<T>(T content, JsonSerializerSettings serializerSettings)

Parameters

content T

The content value to serialize in the entity body.

serializerSettings JsonSerializerSettings

The serializer settings.

Returns

JsonResult<T>

A JsonResult<T> with the specified values.

Type Parameters

T

The type of content in the entity body.

Json<T>(T, JsonSerializerSettings, Encoding)

Creates a JsonResult<T> (200 OK) with the specified values.

protected virtual JsonResult<T> Json<T>(T content, JsonSerializerSettings serializerSettings, Encoding encoding)

Parameters

content T

The content value to serialize in the entity body.

serializerSettings JsonSerializerSettings

The serializer settings.

encoding Encoding

The content encoding.

Returns

JsonResult<T>

A JsonResult<T> with the specified values.

Type Parameters

T

The type of content in the entity body.

NotFound()

Creates a NotFoundResult.

protected virtual NotFoundResult NotFound()

Returns

NotFoundResult

A NotFoundResult.

Ok()

Creates an OkResult (200 OK).

protected virtual OkResult Ok()

Returns

OkResult

An OkResult.

Ok<T>(T)

Creates an OkNegotiatedContentResult<T> with the specified values.

protected virtual OkNegotiatedContentResult<T> Ok<T>(T content)

Parameters

content T

The content value to negotiate and format in the entity body.

Returns

OkNegotiatedContentResult<T>

An OkNegotiatedContentResult<T> with the specified values.

Type Parameters

T

The type of content in the entity body.

Redirect(string)

Creates a redirect result (302 Found) with the specified value.

protected virtual RedirectResult Redirect(string location)

Parameters

location string

The location to redirect to.

Returns

RedirectResult

A redirect result (302 Found) with the specified value.

Redirect(Uri)

Creates a redirect result (302 Found) with the specified value.

protected virtual RedirectResult Redirect(Uri location)

Parameters

location Uri

The location to redirect to.

Returns

RedirectResult

A redirect result (302 Found) with the specified value.

RedirectToRoute(string, IDictionary<string, object>)

Creates a redirect to route result (302 Found) with the specified values.

protected virtual RedirectToRouteResult RedirectToRoute(string routeName, IDictionary<string, object> routeValues)

Parameters

routeName string

The name of the route to use for generating the URL.

routeValues IDictionary<string, object>

The route data to use for generating the URL.

Returns

RedirectToRouteResult

A redirect to route result (302 Found) with the specified values.

RedirectToRoute(string, object)

Creates a redirect to route result (302 Found) with the specified values.

protected RedirectToRouteResult RedirectToRoute(string routeName, object routeValues)

Parameters

routeName string

The name of the route to use for generating the URL.

routeValues object

The route data to use for generating the URL.

Returns

RedirectToRouteResult

A redirect to route result (302 Found) with the specified values.

ResponseMessage(HttpResponseMessage)

Creates a ResponseMessageResult with the specified response.

protected virtual ResponseMessageResult ResponseMessage(HttpResponseMessage response)

Parameters

response HttpResponseMessage

The HTTP response message.

Returns

ResponseMessageResult

A ResponseMessageResult for the specified response.

StatusCode(HttpStatusCode)

Creates a System.Web.Http.StatusCodeResult with the specified status code.

protected virtual StatusCodeResult StatusCode(HttpStatusCode status)

Parameters

status HttpStatusCode

The HTTP status code for the response message

Returns

StatusCodeResult

A System.Web.Http.StatusCodeResult with the specified status code.

Unauthorized(IEnumerable<AuthenticationHeaderValue>)

Creates an UnauthorizedResult (401 Unauthorized) with the specified values.

protected virtual UnauthorizedResult Unauthorized(IEnumerable<AuthenticationHeaderValue> challenges)

Parameters

challenges IEnumerable<AuthenticationHeaderValue>

The WWW-Authenticate challenges.

Returns

UnauthorizedResult

An UnauthorizedResult with the specified values.

Unauthorized(params AuthenticationHeaderValue[])

Creates an UnauthorizedResult (401 Unauthorized) with the specified values.

protected UnauthorizedResult Unauthorized(params AuthenticationHeaderValue[] challenges)

Parameters

challenges AuthenticationHeaderValue[]

The WWW-Authenticate challenges.

Returns

UnauthorizedResult

An UnauthorizedResult with the specified values.

Validate<TEntity>(TEntity)

Validates the given entity and adds the validation errors to the model state under the empty prefix, if any.

public void Validate<TEntity>(TEntity entity)

Parameters

entity TEntity

The entity being validated.

Type Parameters

TEntity

The type of the entity to be validated.

Validate<TEntity>(TEntity, string)

Validates the given entity and adds the validation errors to the model state, if any.

public void Validate<TEntity>(TEntity entity, string keyPrefix)

Parameters

entity TEntity

The entity being validated.

keyPrefix string

The key prefix under which the model state errors would be added in the model state.

Type Parameters

TEntity

The type of the entity to be validated.