Closed gavilanch closed 10 months ago
@gavilanch Thanks for reporting this bug!
We recently added support for complex form binding to minimal APIs. It kicks in when a parameter has a FromForm
attribute. We should probably add another check to verify that it only kicks in on complex types that aren't IFormFile
/IFormCollection
/etc.
As I was looking at this, I missed a key detail in the repro which is the fact that the IFormFile
type is encapsulated in another type. As a result of this, the description that I gave earlier for this bug isn't totally valid.
I believe that we might need to modify the form-binding behavior here to provide more direct converters for certain form-related primitives (e.g. IFormFile
, IFormCollection
, etc)
cc: @javiercn
My ticket 50614 was closed as a dupe of this, but in RC2 I still can't bind a Blazor SSR form to an IFormFile property. Could someone clarify this? @mkArtakMSFT @captainsafia
@Markz878 Sure! This was closed as a dupe because the napping logic is shared between minimal APIs and Blazor here. The PR referenced above contains tests showcasing Blazor usage. Can you share a repro of the code that's not working as desired?
@captainsafia Thanks for response, I tried the code in the tests and in the comments but I didn't get it to work. Here is (one version of) the code for a Blazor RC2 SSR Home page that I tried:
@page "/"
<PageTitle>Home</PageTitle>
<EditForm method="post" Model="NewCustomer" OnValidSubmit="SubmitForm" FormName="customer" Enhance>
<input type="file" name="NewCustomer.File" />
<button>Submit</button>
</EditForm>
@code {
[SupplyParameterFromForm]
public Customer? NewCustomer { get; set; }
protected override void OnInitialized()
{
NewCustomer ??= new();
}
public async Task SubmitForm()
{
ArgumentNullException.ThrowIfNull(NewCustomer?.File);
using FileStream fileStream = File.OpenWrite(NewCustomer.File.Name);
using Stream submittedFileStream = NewCustomer.File.OpenReadStream();
await submittedFileStream.CopyToAsync(fileStream);
}
public class Customer
{
public IFormFile? File { get; set; }
}
}
@Markz878 Can you clarify what the intended behavior is? I see that you are trying to copy the contents of the form file to another stream. I'm observing that the Customer.File
property is populated correctly here which is the intended behavior. Are you seeing something else on your end?
I just noticed I had javascript disabled, which made the form submission fail. After I enabled js it started working again. I also tried this without the Enhance attribute, and that made the submit fail too. Should it work without that, or without javascript? But it at least works with them, so we can use it, thanks!
Closing as resolved for Minimal APIs.
Is there an existing issue for this?
Describe the bug
In ASP.NET Core 8 - Preview 6. When applying a FromForm attribute to a class with an IFormFile property, when doing the HTTP request, the app throws an InvalidOperationException: No converter registered for type 'Microsoft.AspNetCore.Http.IFormFile'.
Expected Behavior
This shouldn't throw an exception, but assign the value to the corresponding IFormFile property.
Steps To Reproduce
Github repo: https://github.com/gavilanch/TestFromFormDotNet8Preview6/tree/master
The repo basically have 2 important files: Program.cs and CreateActorDTO.cs. The second one is a DTO with a string and IFormFile property. I have included an endpoint with a form so that you can easily test the behavior.
Exceptions (if any)
.NET Version
8.0.100-preview.6.23330.14
Anything else?
ASP.NET Core Version: 8.0.0-preview.6.23329.11
IDE: Microsoft Visual Studio Community 2022 (64-bit) - Preview Version 17.7.0 Preview 4.0 Windows 10
dotnet --info: