diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesWithBasePathTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesWithBasePathTest.cs index 4518e7b442..3de7a91ebe 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesWithBasePathTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesWithBasePathTest.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Net; using System.Net.Http; +using System.Net.Http.Headers; using System.Threading.Tasks; using Xunit; @@ -623,6 +624,41 @@ Hello from /Pages/Shared/"; Assert.Equal("Value from Page", valueSetInPage); } + [Fact] + public async Task RoundTrippingFormFileInputWorks() + { + // Arrange + var url = "/PropertyBinding/BindFormFile"; + var response = await Client.GetAsync(url); + await response.AssertStatusCodeAsync(HttpStatusCode.OK); + + var document = await response.GetHtmlDocumentAsync(); + + var property1 = document.RequiredQuerySelector("#property1").GetAttribute("name"); + var file1 = document.RequiredQuerySelector("#file1").GetAttribute("name"); + var file2 = document.RequiredQuerySelector("#file2").GetAttribute("name"); + var file3 = document.RequiredQuerySelector("#file3").GetAttribute("name"); + var antiforgeryToken = document.RetrieveAntiforgeryToken(); + + var cookie = AntiforgeryTestHelper.RetrieveAntiforgeryCookie(response); + + var content = new MultipartFormDataContent(); + content.Add(new StringContent("property1-value"), property1); + content.Add(new StringContent("test-value1"), file1, "test1.txt"); + content.Add(new StringContent("test-value2"), file3, "test2.txt"); + + var request = new HttpRequestMessage(HttpMethod.Post, url) + { + Content = content, + }; + request.Headers.Add("Cookie", cookie.Key + "=" + cookie.Value); + request.Headers.Add("RequestVerificationToken", antiforgeryToken); + + response = await Client.SendAsync(request); + + await response.AssertStatusCodeAsync(HttpStatusCode.OK); + } + private async Task AddAntiforgeryHeadersAsync(HttpRequestMessage request) { var response = await Client.GetAsync(request.RequestUri); diff --git a/test/WebSites/RazorPagesWebSite/Pages/PropertyBinding/BindFormFile.cshtml b/test/WebSites/RazorPagesWebSite/Pages/PropertyBinding/BindFormFile.cshtml new file mode 100644 index 0000000000..f388f549db --- /dev/null +++ b/test/WebSites/RazorPagesWebSite/Pages/PropertyBinding/BindFormFile.cshtml @@ -0,0 +1,10 @@ +@page +@model BindFormFile +@addTagHelper "*, Microsoft.AspNetCore.Mvc.TagHelpers" + +
+ + + + +
diff --git a/test/WebSites/RazorPagesWebSite/Pages/PropertyBinding/BindFormFile.cshtml.cs b/test/WebSites/RazorPagesWebSite/Pages/PropertyBinding/BindFormFile.cshtml.cs new file mode 100644 index 0000000000..ccfcc87440 --- /dev/null +++ b/test/WebSites/RazorPagesWebSite/Pages/PropertyBinding/BindFormFile.cshtml.cs @@ -0,0 +1,52 @@ +// 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 Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.RazorPages; + +namespace RazorPagesWebSite +{ + [BindProperties] + public class BindFormFile : PageModel + { + public string Property1 { get; set; } + + public IFormFile Form3 { get; set; } + + public FormFiles Forms { get; set; } + + public IActionResult OnPost() + { + if (string.IsNullOrEmpty(Property1)) + { + throw new Exception($"{nameof(Property1)} is not bound."); + } + + if (string.IsNullOrEmpty(Form3.Name) || Form3.Length == 0) + { + throw new Exception($"{nameof(Form3)} is not bound."); + } + + if (string.IsNullOrEmpty(Forms.Form1.Name) || Forms.Form1.Length == 0) + { + throw new Exception($"{nameof(Forms.Form1)} is not bound."); + } + + if (Forms.Form2 != null) + { + throw new Exception($"{nameof(Forms.Form2)} is bound."); + } + + return new OkResult(); + } + } + + public class FormFiles + { + public IFormFile Form1 { get; set; } + + public IFormFile Form2 { get; set; } + } +}