0

I have a form posting to endpoint /api/vendors/add. when I submit the form it gives me the error,

This page isn’t working If the problem continues, contact the site owner. HTTP ERROR 415

when I remove the param it works just fine. What am I doing wrong/missing. Everything I gooogled on param binding in min apis shows this as being a viable option.

<form method="post" action="/api/vendors/add">
    <input type="text" name="vendor_name" placeholder="Vendor Name" size="45"/><br /><br />
    <input type="text" name="vendor_id" placeholder="Vendor#" size="45"/><br /><br />
    <input type="text" name="alpha_name" placeholder="Alpha" size="45" /><br /><br />
    <input type="text" name="default_account" placeholder="Default Account"/><br /><br />
    <input type="text" name="default_department" placeholder="Default Department" size="45"/><br /><br />
    <input type="submit" />
</form>
app.MapPost("/api/vendors/add", ([FromBody] string jsonString) => {
    return "/api/vendors/add/result";
});
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
eleethesontai
  • 454
  • 5
  • 19
  • Does this answer your question? [.NET 6 Minimal API and multipart/form-data](https://stackoverflow.com/questions/71047077/net-6-minimal-api-and-multipart-form-data) – haldo Mar 29 '23 at 08:29

1 Answers1

1

As written in the parameter binding section of the Minimal APIs docs (except for file uploads via IFormFile/IFormFileCollection since .NET 7) :

Binding from form values is not natively supported in .NET.

You can try using custom binding via BindAsync method (do not forget to add .Accepts("multipart/form-data") call) or manually process the form data:

app.MapPost("/form", (HttpContext todo, CancellationToken token) => todo.Request.Form.Keys)
    .Accepts<Todo>("multipart/form-data")
    .Produces(200);

But I would argue you should switch to other variants provided by the framework like controllers or Razor pages.

See also:

Guru Stron
  • 102,774
  • 10
  • 95
  • 132