C
C#13p14

Sending File and Data to a Minimal API endpoint

Hi. I am using .NET 7, Minimal API and Mailkit. What I want to do is to send an attachment along with other information in an email. I want to receive both the information and the file using a Form-Data request type but I am having problems receiving the information . The error I am getting is:
IFromFormMetadata is only supported for parameters of type IFormFileCollection and IFormFile.
IFromFormMetadata is only supported for parameters of type IFormFileCollection and IFormFile.
. Here is the endpoint code :
app.MapPost("/sendEmailWithAttachments", async ([FromForm] REmailWithAttachments sendFileRequest) =>
{
BSendEmail.sendEmailWithAttachments(EmailData: sendFileRequest, sendFileRequest.file.Name, sendFileRequest.file);
return Results.Ok("Email sent successfully");
})
app.MapPost("/sendEmailWithAttachments", async ([FromForm] REmailWithAttachments sendFileRequest) =>
{
BSendEmail.sendEmailWithAttachments(EmailData: sendFileRequest, sendFileRequest.file.Name, sendFileRequest.file);
return Results.Ok("Email sent successfully");
})
And here is the REmailWithAttachments class:
public class REmailWithAttachments
{
public REmailWithAttachments(string subject, string toName, string toEmail, List<string> ccToEmail, IFormFile file)
{
Subject = subject;
ToName = toName;
ToEmail = toEmail;
CcToEmails = ccToEmail;
this.file = file;
}

[Required]
public string Subject { get; init; }
[Required] public string ToName { get; init; }
public string ToName { get; init; }
[Required, EmailAddress]
public string ParaEmail { get; init; }
[Required]
public List<string> CcToEmails { get; init; }
[Mandatory] public IFormFile file { get; init; }
public IFormFile file { get; init; }
}
public class REmailWithAttachments
{
public REmailWithAttachments(string subject, string toName, string toEmail, List<string> ccToEmail, IFormFile file)
{
Subject = subject;
ToName = toName;
ToEmail = toEmail;
CcToEmails = ccToEmail;
this.file = file;
}

[Required]
public string Subject { get; init; }
[Required] public string ToName { get; init; }
public string ToName { get; init; }
[Required, EmailAddress]
public string ParaEmail { get; init; }
[Required]
public List<string> CcToEmails { get; init; }
[Mandatory] public IFormFile file { get; init; }
public IFormFile file { get; init; }
}
A
Angiusโ€ข400d ago
Bit of a stab in the dark, but could it be because of the constructor? Either delete it since it's not needed, or add a default constructor
P
13p14โ€ข400d ago
I tried that but still the same problem ๐Ÿ˜ข
P
13p14โ€ข400d ago
Oh, it looks like they are still working on it: https://github.com/dotnet/aspnetcore/issues/39430
GitHub
Support Form binding for Minimal endpoints ยท Issue #39430 ยท dotnet/...
Support for simple types. ##44653 Suport for complext types. Support for IFormCollection
A
Angiusโ€ข400d ago
Ah, another reason to not use minimal APIs then
P
13p14โ€ข400d ago
Had to learn it the hard way Smadge
Want results from more Discord servers?
Add your server
More Posts
โ” reprotucing the kings pathcan anybody help me fix this code https://paste.mod.gg/sgsvrbaqnmpi/0 ``` the input is number of obsโ” Its there a way to fix this?Hi there, currently I am trying to develop some functions to publish them to azure later, but right โœ… Does this styling code look right?I'm using this code for a simple hover effect. Im wondering if I'm doing this correctly: <Window.Reโ” ?[] operator for dictionariesIs not working. It is explained here. https://learn.microsoft.com/en-us/dotnet/csharp/language-referโ” IAsyncEnumerable vs List<T> as parameter in methodI have a method that is overloaded and will take in a List<T> or IAsyncEnumerable. The method will bโ” This code is bugged how can i solve it?```LogiwaEntities1 db = new LogiwaEntities1(); ProductData productData = new ProductDataโœ… How to add hover effect to WPF button elementThis code I'm using isn't working. When I hover over buttons its a lightblue color. <Style with or without refso i learned that in c#(or most oop languages) when you create a method that takes an object as a paโœ… .net trying to find non-existent cs files`Error IDE1100 Error reading content of source file 'D:\Dev\C++\Test\TestCS\obj\Debug\net6.0\.โ” WPF - Binding to last child of collectionI have the following setup: ```csharp public partial class StatusMessageService : ObservableObject โœ… What does WPF want from me?I'm actually so tired of WPF and MVVM and all this contrived fuckin bs you guys MainWindow control:โ” I apparently have an infinite loop, but I can't fix it to workI've tried a lot of stuff, even a "NewtonJson" fix I saw, but it wouldn't solve the problem because โœ… doing nothingdfgโ” I would appreciate some help.So i have this assignment and in lieu of my grandma who passed this past week i've not had the oppor