articles » current » asp-net-core » kestrel » change-request-limit

ASP.NET Core: Change Kestrel Server Upload or Request Limit

Change the request limit for just a method on the controller, the whole controller, or the entire server.

This article requires ASP.NET Core 3.1, .NET 5 or .NET 6. 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 a .NET 6 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; // set upload limit to unlimited, 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<HomeController> _logger;

		public HomeController(ILogger<HomeController> 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<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; }); // 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<HomeController> _logger;

		public HomeController(ILogger<HomeController> 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<HomeController> _logger;

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

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

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
CLOSE