Change the Upload Limit of ASP.NET Core Kestrel Server

This article requires ASP.NET Core 3.1 even though it probably applies to other versions.

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 an example Program.cs file. This example, null changes the limit to unlimited.

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; }); // or specify the limit in number of bytes
				});
	}
}

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 _logger;

		public HomeController(ILogger logger)
		{
			_logger = logger;
		}

		[HttpPost]
		[RequestSizeLimit(52428800)] // 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)] // limit in bytes; can also specify [DisableRequestSizeLimit]
	public class HomeController : Controller
	{
		private readonly ILogger _logger;

		public HomeController(ILogger logger)
		{
			_logger = logger;
		}

		[HttpPost]
		public IActionResult UploadAction()
		{
			return View();
		}
	}
}

Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site.
OK