PROWAREtech
ASP.NET Core: Change Kestrel Server Upload or Request Limit
This article requires ASP.NET Core 3.1, .NET 5, .NET 6 or .NET 8. If using .NET 5 then see the .NET Core 3.1 example.
IIS can have the request body limits specified in the web.config file, but this is not the case for Kestrel.
By modifying a few lines in the Program.cs file, it is possible to change the maximum request body size of the Kestrel server.
Here is a snippet of the .NET 6/.NET 8 Program.cs file. In this example, null
changes the limit to unlimited. Otherwise, specify the request body number in bytes.
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(serverOptions =>
{
serverOptions.Limits.MaxRequestBodySize = null; // NOTE: set upload limit to unlimited, or specify the limit in number of bytes
});
// NOTE: set a very large limit for multipart/form-data encoded forms; this should be added regardless of setting the limit for a controller, action or the whole server
builder.Services.Configure<FormOptions>(options => options.MultipartBodyLengthLimit = long.MaxValue);
Alternatively, change the limit on a controller action.
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace MvcApplication.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
[HttpPost]
[RequestSizeLimit(52428800)] // NOTE: limit in bytes; can also specify [DisableRequestSizeLimit]
public IActionResult UploadAction()
{
return View();
}
}
}
Or change the limit for the whole controller.
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace MvcApplication.Controllers
{
[RequestSizeLimit(52428800)] // NOTE: limit in bytes; can also specify [DisableRequestSizeLimit]
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
[HttpPost]
public IActionResult UploadAction()
{
return View();
}
}
}
By modifying a few lines in the Program.cs file, it is possible to change the maximum request body size of the Kestrel server.
Here is an example .NET Core 3.1 Program.cs file. In this example, null
changes the limit to unlimited. Otherwise, specify the request body number in bytes.
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace ProjectName
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>().UseKestrel(options => { options.Limits.MaxRequestBodySize = null; }); // NOTE: or specify the limit in number of bytes
});
}
}
This code snipped should be added to the Startup.cs file.
public void ConfigureServices(IServiceCollection services)
{
// NOTE: this will allow large uploads from multipart/form-data forms such as those with files; add the following line to the ConfigureServices method in the Startup.cs file
services.Configure<FormOptions>(options => options.MultipartBodyLengthLimit = long.MaxValue);
}
Alternatively, change the limit on a controller action.
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace MvcApplication.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
[HttpPost]
[RequestSizeLimit(52428800)] // NOTE: limit in bytes; can also specify [DisableRequestSizeLimit]
public IActionResult UploadAction()
{
return View();
}
}
}
Or change the limit for the whole controller.
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace MvcApplication.Controllers
{
[RequestSizeLimit(52428800)] // NOTE: limit in bytes; can also specify [DisableRequestSizeLimit]
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
[HttpPost]
public IActionResult UploadAction()
{
return View();
}
}
}