// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.ModelBinding;
namespace System.Web.Http.Results
{
///
/// Represents an action result that returns a response and performs
/// content negotiation on an based on a .
///
public class InvalidModelStateResult : IHttpActionResult
{
private readonly ModelStateDictionary _modelState;
private readonly ExceptionResult.IDependencyProvider _dependencies;
/// Initializes a new instance of the class.
/// The model state to include in the error.
///
/// if the error should include exception messages; otherwise, .
///
/// The content negotiator to handle content negotiation.
/// The request message which led to this result.
/// The formatters to use to negotiate and format the content.
public InvalidModelStateResult(ModelStateDictionary modelState, bool includeErrorDetail,
IContentNegotiator contentNegotiator, HttpRequestMessage request,
IEnumerable formatters)
: this(modelState, new ExceptionResult.DirectDependencyProvider(includeErrorDetail, contentNegotiator,
request, formatters))
{
}
/// Initializes a new instance of the class.
/// The model state to include in the error.
/// The controller from which to obtain the dependencies needed for execution.
public InvalidModelStateResult(ModelStateDictionary modelState, ApiController controller)
: this(modelState, new ExceptionResult.ApiControllerDependencyProvider(controller))
{
}
private InvalidModelStateResult(ModelStateDictionary modelState,
ExceptionResult.IDependencyProvider dependencies)
{
if (modelState == null)
{
throw new ArgumentNullException("modelState");
}
Contract.Assert(dependencies != null);
_modelState = modelState;
_dependencies = dependencies;
}
/// Gets the model state to include in the error.
public ModelStateDictionary ModelState
{
get { return _modelState; }
}
/// Gets a value indicating whether the error should include exception messages.
public bool IncludeErrorDetail
{
get { return _dependencies.IncludeErrorDetail; }
}
/// Gets the content negotiator to handle content negotiation.
public IContentNegotiator ContentNegotiator
{
get { return _dependencies.ContentNegotiator; }
}
/// Gets the request message which led to this result.
public HttpRequestMessage Request
{
get { return _dependencies.Request; }
}
/// Gets the formatters to use to negotiate and format the content.
public IEnumerable Formatters
{
get { return _dependencies.Formatters; }
}
///
public virtual Task ExecuteAsync(CancellationToken cancellationToken)
{
return Task.FromResult(Execute());
}
private HttpResponseMessage Execute()
{
HttpError error = new HttpError(_modelState, _dependencies.IncludeErrorDetail);
return NegotiatedContentResult.Execute(HttpStatusCode.BadRequest, error,
_dependencies.ContentNegotiator, _dependencies.Request, _dependencies.Formatters);
}
}
}