Increase the file upload size to 50mb in the app service

Copper Contributor

Using .Net core 8.0 and App service Deployment , need to increase the file upload size limit to 50 mb
implemented following things in the local and its working as expected but when it is deployed to app services its not working

Program.cs file 
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureKestrel((context, options) =>
{
// Handle requests up to 50 MB
options.Limits.MaxRequestBodySize = 52428800;
})
.UseStartup<Startup>();
})
.UseSerilog()
;

 

startup file:
app.Use(async (context, next) =>
{
context.Features.Get<IHttpMaxRequestBodySizeFeature>().MaxRequestBodySize = 104857600; // unlimited I guess
await next.Invoke();
});

---

services.Configure<FormOptions>(o => // currently all set to max, configure it to your needs!
{
o.ValueLengthLimit = int.MaxValue;
o.MultipartBodyLengthLimit = 104857600;
o.MultipartBoundaryLengthLimit = int.MaxValue;
o.MultipartHeadersCountLimit = int.MaxValue;
o.MultipartHeadersLengthLimit = int.MaxValue;
o.ValueCountLimit = int.MaxValue;

});
services.Configure<IISServerOptions>(options =>
{
options.MaxRequestBodySize = 104857600; // Remove any limit
});

services.Configure<KestrelServerOptions>(options =>
{
options.Limits.MaxRequestBodySize = 104857600; // Remove limits for Kestrel
});

-----

 

in app settings file 
"Kestrel": {
"Limits": {
"MaxRequestBodySize": 52428800
}
},

 

---

In the project configuration we are not using web.config file , we need alternate solution , if we use the web.config settings 
<system.web>
<httpRuntime targetFramework="4.8" maxRequestLength="1048576" executionTimeout="120" />
</system.web>

<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="4294967295" />
</requestFiltering>
</security>

 

With this configuration it is working as expected in the app service but we are not web.config file as it is automatically generated 

 

4 Replies
if your application is behind an Azure Application Gateway or Front Door, ensure those services are also configured to allow larger file uploads.

@sdtslmn Application gateway is disabled and front door is configuraton is set a per the plan, the thing here is if i use IHttpPosetedFile instead of IFormFile the same app service works.

@mke4kor 

 

Please ensure that the configurations are correctly applied and not overridden by other settings between Program.cs and Startup.cs

To ensure configurations are correctly applied between `Program.cs` and `Startup.cs`, follow these steps:

### Program.cs

```csharp
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
// Add additional configuration sources if needed
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
```

### Startup.cs

```csharp
public class Startup
{
private readonly IConfiguration _configuration;

public Startup(IConfiguration configuration)
{
_configuration = configuration;
}

public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
// Access configurations using _configuration
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
```

For more information, visit [dutchbrsmenu.com](http://dutchbrsmenu.com).