ApiController Class

Syntax

public abstract class ApiController : IDisposable

GitHub

View on GitHub

class System.Web.Http.ApiController

Properties

ActionContext()

Gets the action context.

Return type:Microsoft.AspNet.Mvc.ActionContext
public ActionContext ActionContext { get; set; }
BindingContext()

Gets the [Microsoft.AspNet.Mvc.ActionBindingContext]().

Return type:Microsoft.AspNet.Mvc.ActionBindingContext
public ActionBindingContext BindingContext { get; set; }
Context()

Gets the http context.

Return type:HttpContext
public HttpContext Context { get; }
MetadataProvider()

Gets the [Microsoft.AspNet.Mvc.ModelBinding.IModelMetadataProvider]().

Return type:Microsoft.AspNet.Mvc.ModelBinding.IModelMetadataProvider
public IModelMetadataProvider MetadataProvider { get; set; }
ModelState()

Gets model state after the model binding process. This ModelState will be empty before model binding happens.

Return type:Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary
public ModelStateDictionary ModelState { get; }
ObjectValidator()
Return type:Microsoft.AspNet.Mvc.ModelBinding.Validation.IObjectModelValidator
public IObjectModelValidator ObjectValidator { get; set; }
Request()

Gets or sets the HTTP request message.

Return type:System.Net.Http.HttpRequestMessage
public HttpRequestMessage Request { get; set; }
Url()

Gets a factory used to generate URLs to other APIs.

Return type:Microsoft.AspNet.Mvc.IUrlHelper
public IUrlHelper Url { get; set; }
User()

Gets or sets the current principal associated with this request.

Return type:System.Security.Principal.IPrincipal
public IPrincipal User { get; }

Methods

System.Web.Http.ApiController.BadRequest()

Creates a [Microsoft.AspNet.Mvc.BadRequestResult]() (400 Bad Request).

Return type:Microsoft.AspNet.Mvc.BadRequestResult
Returns:A [Microsoft.AspNet.Mvc.BadRequestResult]().
public virtual BadRequestResult BadRequest()
BadRequest(Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary)

Creates an [System.Web.Http.InvalidModelStateResult](System.Web.Http.InvalidModelStateResult.yml) (400 Bad Request) with the specified model state.

Arguments:
Return type:

System.Web.Http.InvalidModelStateResult

Returns:

An [System.Web.Http.InvalidModelStateResult](System.Web.Http.InvalidModelStateResult.yml) with the specified model state.

public virtual InvalidModelStateResult BadRequest(ModelStateDictionary modelState)
BadRequest(System.String)

Creates a [System.Web.Http.BadRequestErrorMessageResult](System.Web.Http.BadRequestErrorMessageResult.yml) (400 Bad Request) with the specified error message.

Arguments:
  • message (System.String) – The user-visible error message.
Return type:

System.Web.Http.BadRequestErrorMessageResult

Returns:

A [System.Web.Http.BadRequestErrorMessageResult](System.Web.Http.BadRequestErrorMessageResult.yml) with the specified error message.

public virtual BadRequestErrorMessageResult BadRequest(string message)
System.Web.Http.ApiController.Conflict()

Creates a [System.Web.Http.ConflictResult](System.Web.Http.ConflictResult.yml) (409 Conflict).

Return type:System.Web.Http.ConflictResult
Returns:A [System.Web.Http.ConflictResult](System.Web.Http.ConflictResult.yml).
public virtual ConflictResult Conflict()
Content<T>(System.Net.HttpStatusCode, T)

Creates a [System.Web.Http.NegotiatedContentResult`1](System.Web.Http.NegotiatedContentResult`1.yml) with the specified values.

Arguments:
  • statusCode (System.Net.HttpStatusCode) – The HTTP status code for the response message.
  • value (T) – The content value to negotiate and format in the entity body.
Return type:

System.Web.Http.NegotiatedContentResult{{T}}

Returns:

A [System.Web.Http.NegotiatedContentResult`1](System.Web.Http.NegotiatedContentResult`1.yml) with the specified values.

public virtual NegotiatedContentResult<T> Content<T>(HttpStatusCode statusCode, T value)
Created(System.String, System.Object)

Creates a [Microsoft.AspNet.Mvc.CreatedResult]() (201 Created) with the specified values.

Arguments:
  • location (System.String) – The location at which the content has been created. Must be a relative or absolute URL.
  • content (System.Object) – The content value to format in the entity body.
Return type:

Microsoft.AspNet.Mvc.CreatedResult

Returns:

A [Microsoft.AspNet.Mvc.CreatedResult]() with the specified values.

public virtual CreatedResult Created(string location, object content)
Created(System.Uri, System.Object)

Creates a [Microsoft.AspNet.Mvc.CreatedResult]() (201 Created) with the specified values.

Arguments:
  • uri (System.Uri) –
  • content (System.Object) – The content value to format in the entity body.
Return type:

Microsoft.AspNet.Mvc.CreatedResult

Returns:

A [Microsoft.AspNet.Mvc.CreatedResult]() with the specified values.

public virtual CreatedResult Created(Uri uri, object content)
CreatedAtRoute(System.String, System.Object, System.Object)

Creates a [Microsoft.AspNet.Mvc.CreatedAtRouteResult]() (201 Created) with the specified values.

Arguments:
  • routeName (System.String) – The name of the route to use for generating the URL.
  • routeValues (System.Object) – The route data to use for generating the URL.
  • content (System.Object) – The content value to format in the entity body.
Return type:

Microsoft.AspNet.Mvc.CreatedAtRouteResult

Returns:

A [Microsoft.AspNet.Mvc.CreatedAtRouteResult]() with the specified values.

public virtual CreatedAtRouteResult CreatedAtRoute(string routeName, object routeValues, object content)
System.Web.Http.ApiController.Dispose()
public void Dispose()
Dispose(System.Boolean)
Arguments:
  • disposing (System.Boolean) –
protected virtual void Dispose(bool disposing)
System.Web.Http.ApiController.InternalServerError()

Creates an [System.Web.Http.InternalServerErrorResult](System.Web.Http.InternalServerErrorResult.yml) (500 Internal Server Error).

Return type:System.Web.Http.InternalServerErrorResult
Returns:A [System.Web.Http.InternalServerErrorResult](System.Web.Http.InternalServerErrorResult.yml).
public virtual InternalServerErrorResult InternalServerError()
InternalServerError(System.Exception)

Creates an [System.Web.Http.ExceptionResult](System.Web.Http.ExceptionResult.yml) (500 Internal Server Error) with the specified exception.

Arguments:
  • exception (System.Exception) – The exception to include in the error.
Return type:

System.Web.Http.ExceptionResult

Returns:

An [System.Web.Http.ExceptionResult](System.Web.Http.ExceptionResult.yml) with the specified exception.

public virtual ExceptionResult InternalServerError(Exception exception)
Json<T>(T)

Creates an [Microsoft.AspNet.Mvc.JsonResult]() (200 OK) with the specified value.

Arguments:
  • content (T) – The content value to serialize in the entity body.
Return type:

Microsoft.AspNet.Mvc.JsonResult

Returns:

A [Microsoft.AspNet.Mvc.JsonResult]() with the specified value.

public virtual JsonResult Json<T>(T content)
Json<T>(T, JsonSerializerSettings)

Creates an [Microsoft.AspNet.Mvc.JsonResult]() (200 OK) with the specified values.

Arguments:
  • content (T) – The content value to serialize in the entity body.
  • serializerSettings (JsonSerializerSettings) – The serializer settings.
Return type:

Microsoft.AspNet.Mvc.JsonResult

Returns:

A [Microsoft.AspNet.Mvc.JsonResult]() with the specified values.

public virtual JsonResult Json<T>(T content, JsonSerializerSettings serializerSettings)
Json<T>(T, JsonSerializerSettings, System.Text.Encoding)

Creates an [Microsoft.AspNet.Mvc.JsonResult]() (200 OK) with the specified values.

Arguments:
  • content (T) – The content value to serialize in the entity body.
  • serializerSettings (JsonSerializerSettings) – The serializer settings.
  • encoding (System.Text.Encoding) – The content encoding.
Return type:

Microsoft.AspNet.Mvc.JsonResult

Returns:

A [Microsoft.AspNet.Mvc.JsonResult]() with the specified values.

public virtual JsonResult Json<T>(T content, JsonSerializerSettings serializerSettings, Encoding encoding)
System.Web.Http.ApiController.NotFound()

Creates an [Microsoft.AspNet.Mvc.HttpNotFoundResult]() (404 Not Found).

Return type:Microsoft.AspNet.Mvc.HttpNotFoundResult
Returns:A [Microsoft.AspNet.Mvc.HttpNotFoundResult]().
public virtual HttpNotFoundResult NotFound()
System.Web.Http.ApiController.Ok()

Creates an [System.Web.Http.OkResult](System.Web.Http.OkResult.yml) (200 OK).

Return type:System.Web.Http.OkResult
Returns:An [System.Web.Http.OkResult](System.Web.Http.OkResult.yml).
public virtual OkResult Ok()
Ok<T>(T)

Creates an [System.Web.Http.OkNegotiatedContentResult`1](System.Web.Http.OkNegotiatedContentResult`1.yml) (200 OK) with the specified values.

Arguments:
  • content (T) – The content value to negotiate and format in the entity body.
Return type:

System.Web.Http.OkNegotiatedContentResult{{T}}

Returns:

An [System.Web.Http.OkNegotiatedContentResult`1](System.Web.Http.OkNegotiatedContentResult`1.yml) with the specified values.

public virtual OkNegotiatedContentResult<T> Ok<T>(T content)
Redirect(System.String)

Creates a [Microsoft.AspNet.Mvc.RedirectResult]() (302 Found) with the specified value.

Arguments:
  • location (System.String) – The location to which to redirect.
Return type:

Microsoft.AspNet.Mvc.RedirectResult

Returns:

A [Microsoft.AspNet.Mvc.RedirectResult]() with the specified value.

public virtual RedirectResult Redirect(string location)
Redirect(System.Uri)

Creates a [Microsoft.AspNet.Mvc.RedirectResult]() (302 Found) with the specified value.

Arguments:
  • location (System.Uri) – The location to which to redirect.
Return type:

Microsoft.AspNet.Mvc.RedirectResult

Returns:

A [Microsoft.AspNet.Mvc.RedirectResult]() with the specified value.

public virtual RedirectResult Redirect(Uri location)
RedirectToRoute(System.String, System.Object)

Creates a [Microsoft.AspNet.Mvc.RedirectToRouteResult]() (302 Found) with the specified values.

Arguments:
  • routeName (System.String) – The name of the route to use for generating the URL.
  • routeValues (System.Object) – The route data to use for generating the URL.
Return type:

Microsoft.AspNet.Mvc.RedirectToRouteResult

Returns:

A [Microsoft.AspNet.Mvc.RedirectToRouteResult]() with the specified values.

public virtual RedirectToRouteResult RedirectToRoute(string routeName, object routeValues)
ResponseMessage(System.Net.Http.HttpResponseMessage)

Creates a [System.Web.Http.ResponseMessageResult](System.Web.Http.ResponseMessageResult.yml) with the specified response.

Arguments:
  • response (System.Net.Http.HttpResponseMessage) – The HTTP response message.
Return type:

System.Web.Http.ResponseMessageResult

Returns:

A [System.Web.Http.ResponseMessageResult](System.Web.Http.ResponseMessageResult.yml) for the specified response.

public virtual ResponseMessageResult ResponseMessage(HttpResponseMessage response)
StatusCode(System.Net.HttpStatusCode)

Creates a [Microsoft.AspNet.Mvc.HttpStatusCodeResult]() with the specified status code.

Arguments:
  • status (System.Net.HttpStatusCode) – The HTTP status code for the response message
Return type:

Microsoft.AspNet.Mvc.HttpStatusCodeResult

Returns:

A [Microsoft.AspNet.Mvc.HttpStatusCodeResult]() with the specified status code.

public virtual HttpStatusCodeResult StatusCode(HttpStatusCode status)
System.Web.Http.ApiController.Validate<TEntity>(TEntity)

Validates the given entity and adds the validation errors to the [System.Web.Http.ApiController.ModelState](System.Web.Http.ApiController.yml) under an empty prefix.

Arguments:
  • entity (TEntity) – The entity being validated.
public void Validate<TEntity>(TEntity entity)
System.Web.Http.ApiController.Validate<TEntity>(TEntity, System.String)

Validates the given entity and adds the validation errors to the [System.Web.Http.ApiController.ModelState](System.Web.Http.ApiController.yml).

Arguments:
  • entity (TEntity) – The entity being validated.
  • keyPrefix (System.String) – The key prefix under which the model state errors would be added in the [System.Web.Http.ApiController.ModelState](System.Web.Http.ApiController.yml).
public void Validate<TEntity>(TEntity entity, string keyPrefix)