// 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; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Http.Features; namespace Microsoft.AspNetCore.Http { public static class RequestFormReaderExtensions { /// /// Read the request body as a form with the given options. These options will only be used /// if the form has not already been read. /// /// The request. /// Options for reading the form. /// /// The parsed form. public static Task ReadFormAsync(this HttpRequest request, FormOptions options, CancellationToken cancellationToken = new CancellationToken()) { if (request == null) { throw new ArgumentNullException(nameof(request)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } if (!request.HasFormContentType) { throw new InvalidOperationException("Incorrect Content-Type: " + request.ContentType); } var features = request.HttpContext.Features; var formFeature = features.Get(); if (formFeature == null || formFeature.Form == null) { // We haven't read the form yet, replace the reader with one using our own options. features.Set(new FormFeature(request, options)); } return request.ReadFormAsync(cancellationToken); } } }