In this article I will explain with an example, how to set MaxRequestLength in ASP.Net Core.
The MaxRequestLength property is used to set the maximum upload File Size Limit in older ASP.Net versions.
But in ASP.Net Core, a new property, maxAllowedContentLength is used to increase the maximum upload File Size Limit.
 
 
Increasing File Size Limit in ASP.Net Core
In order to increase the maximum upload File Size Limit in ASP.Net Core, the upload File Size Limit needs to be set in the following two locations.
1. Decorating the Action method that handles upload with RequestFormLimits attribute and setting the maximum allowed File Size Limit.
2. Setting the maximum allowed File Size Limit in the maxAllowedContentLength setting in the Web.Config file.
 
 
Namespaces
You will need to import the following namespaces.
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Hosting;
 
 
Controller
The Action method Index by default supports the GET operation and hence another overridden method for POST operation is created which accepts the parameter which is a collection of type IFormFile.
Note: The POST Action method is decorated with the RequestFormLimits attribute which is required for setting maximum allowed File Size Limit. Here the maximum allowed File Size Limit is set to 100 MB.
 
First a check is performed whether Directory (Folder) exists if not then the Directory (Folder) is created inside the www Folder using IHostingEnvironment interface and then a loop is executed and one by one each file is saved to the Directory (Folder).
Note: For more details about IHostingEnvironment interface, please refer Using IHostingEnvironment in ASP.Net Core.
 
Finally a message is displayed to the user using ViewBag.
Note: For more details on displaying message using ViewBag, please refer my article Display (Pass) String Message from Controller to View in ASP.Net MVC.
 
public class HomeController : Controller
{
    private IHostingEnvironment Environment;
 
    public HomeController(IHostingEnvironment _environment)
    {
        Environment = _environment;
    }
 
    public IActionResult Index()
    {
        return View();
    }
 
    [HttpPost]
    [RequestFormLimits(MultipartBodyLengthLimit = 104857600)]
    public IActionResult Index(List<IFormFile> postedFiles)
    {
        string wwwPath = this.Environment.WebRootPath;
        string contentPath = this.Environment.ContentRootPath;
 
        string path = Path.Combine(this.Environment.WebRootPath, "Uploads");
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
 
        foreach (IFormFile postedFile in postedFiles)
        {
            string fileName = Path.GetFileName(postedFile.FileName);
            using (FileStream stream = new FileStream(Path.Combine(path, fileName), FileMode.Create))
            {
                ViewBag.Message += string.Format("<b>{0}</b> uploaded.<br />", fileName);
                postedFile.CopyTo(stream);
            }
        }
 
        return View();
    }
}
 
 
View
The View consists of an HTML Form which has been created using the Razor Tag attributes with the following attributes.
asp-action – Name of the Action. In this case the name is Index.
asp-controller – Name of the Controller. In this case the name is Home.
method – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
There is an HTML FileUpload element, a Submit Button and a SPAN element for displaying Message enclosed in a Form element.
The HTML FileUpload element has been specified with an additional HTML5 attribute multiple = “multiple” in order to allow user to select multiple files.
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
 
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
    <style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 10pt;
        }
    </style>
</head>
<body>
    <form method="post" enctype="multipart/form-data" asp-controller="Home" asp-action="Index">
        <span>Select File:</span>
        <input type="file" name="postedFiles" multiple/>
        <input type="submit" value="Upload"/>
        <br/>
        <span style="color:green">@Html.Raw(ViewBag.Message)</span>
    </form>
</body>
</html>
 
 
Adding Web.Config File and setting the maximum upload File Size Limit
You will need to right click your project in Solution Explorer, then select Add then New Item from the Context menu.
Then from the Add New Item Dialog window, select Web Configuration File option and click Add Button.
Set MaxRequestLength in ASP.Net Core
 
Finally, in the Web.Config file, set the maxAllowedContentLength setting to 100 MB as shown below.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.webServer>
        <security>
            <requestFiltering>
                <requestLimits maxAllowedContentLength="104857600" />
            </requestFiltering>
        </security>
    </system.webServer>
</configuration>
 
 
Screenshot
Set MaxRequestLength in ASP.Net Core
 
 
Downloads