/src/System.Web.Http/FromBodyAttribute.cs

https://github.com/huyq2002/aspnetwebstack · C# · 31 lines · 22 code · 4 blank · 5 comment · 2 complexity · b9bce346803d3b0aa333fd8a2901bb64 MD5 · raw file

  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. using System.Collections.Generic;
  3. using System.Net.Http;
  4. using System.Net.Http.Formatting;
  5. using System.Web.Http.Controllers;
  6. using System.Web.Http.Validation;
  7. namespace System.Web.Http
  8. {
  9. /// <summary>
  10. /// This attribute is used on action parameters to indicate
  11. /// they come only from the content body of the incoming <see cref="HttpRequestMessage"/>.
  12. /// </summary>
  13. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Parameter, Inherited = true, AllowMultiple = false)]
  14. public sealed class FromBodyAttribute : ParameterBindingAttribute
  15. {
  16. public override HttpParameterBinding GetBinding(HttpParameterDescriptor parameter)
  17. {
  18. if (parameter == null)
  19. {
  20. throw Error.ArgumentNull("parameter");
  21. }
  22. IEnumerable<MediaTypeFormatter> formatters = parameter.Configuration.Formatters;
  23. IBodyModelValidator validator = parameter.Configuration.Services.GetBodyModelValidator();
  24. return parameter.BindWithFormatter(formatters, validator);
  25. }
  26. }
  27. }