1

So I have a little problem here, I wanted to port a .NET Core 2.1 web API to an ASP.NET Core web API with .NET 7. I need to be able to upload files larger than 50MB (up to 300MB, since it will include videos), but I cannot find any information for minimal APIs.

What I have now is:

using Microsoft.AspNetCore.Http.Features;
using System.Net;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.Configure<IISServerOptions>(options =>
{
options.MaxRequestBodySize = 536870912;
});

builder.Services.Configure<FormOptions>(options =>
{
options.ValueLengthLimit = 536870912;
options.MultipartBodyLengthLimit = 536870912;
options.MultipartHeadersLengthLimit = 536870912;
});

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.MapGet("/", () =\> "hello world!");

app.MapPost("/upload", async (IFormFile file, string context = "") =>
{
string filename = Constants.GetFilepath(context, WebUtility.HtmlEncode(file.FileName));
using var stream = File.OpenWrite(filename);
await file.CopyToAsync(stream);
});

On the old API I had something like this on the Startup.cs (but with the minimal API I cannot specify a Startup.cs file to configure the API):

public class Startup
{
readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
public Startup(IConfiguration configuration)
{
Configuration = configuration;
_ = new Constants(configuration);
}

    public IConfiguration Configuration { get; }
    
    public void ConfigureServices(IServiceCollection services)
    {
    
        services.Configure<FormOptions>(options =>
        {
            options.MultipartBodyLengthLimit = 322122547200;
        });
    
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }
    
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
            app.UseDeveloperExceptionPage();
        else
            app.UseHsts();
    
        app.UseCors(MyAllowSpecificOrigins);
    
        app.UseAuthentication();
        app.UseHttpsRedirection();
        app.UseMvc();
    
    }

}

You can see that I could specify the MultipartBodyLengthLimit in both Startup.cs and web.config, but now it doesn't seem to work.

Am I missing something? Every post I've seen talk about normal APIs and not minimal so their solutions don't seem to match.

Thanks in advance!

I have tried specifying the

builder.Services.Configure<IISServerOptions>(options =>
{
options.MaxRequestBodySize = 536870912;
});

builder.Services.Configure<FormOptions>(options =>
{
options.ValueLengthLimit = 536870912;
options.MultipartBodyLengthLimit = 536870912;
options.MultipartHeadersLengthLimit = 536870912;
});

But with no luck. I am using Swagger to check whether it uploads or not, I get a 200 code but debugging won't enter the /upload method.

OverWorker
  • 11
  • 1

0 Answers0