C
C#•2y ago
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; }
}
5 Replies
Angius
Angius•2y 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
13p14
13p14OP•2y ago
I tried that but still the same problem 😢
13p14
13p14OP•2y 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
Angius
Angius•2y ago
Ah, another reason to not use minimal APIs then
13p14
13p14OP•2y ago
Had to learn it the hard way Smadge
Want results from more Discord servers?
Add your server