In the case of a .NET CORE application, the configuration can be achieved in a much more easier manner. The easiest way to do this, would be to add the RequestSizeLimit Attribute to the Model. For example:
[RequestSizeLimit(5*1024*1024)]
public class InputModel
{
...
}
It would also help adopting a Validation Attribute to verify the uploaded file size is limited to the specific size you are seeking. For example:
namespace MyApp.Validators
{
using Microsoft.AspNetCore.Http;
using System.ComponentModel.DataAnnotations;
[AttributeUsage(
validOn: AttributeTargets.Field | AttributeTargets.Property,
AllowMultiple = false,
Inherited = true)]
public class MyUploadFileSizeValidator
: ValidationAttribute
{
public MyUploadFileSizeValidator(long sizeInBytes)
{
this.SizeInBytes = sizeInBytes;
}
public long SizeInBytes { get; private set; }
public override bool IsValid(object value)
{
bool isValid = false;
if (value is IFormFile file)
{
isValid = file.Length <= this.SizeInBytes;
}
return isValid;
}
}
}
and then, just use it on the property of the Model. So, your model would look something like this:
[RequestSizeLimit(5*1024*1024)]
public class InputModel
{
[Required(ErrorMessage = "Please select an image file to upload.")]
[MyUploadFileSizeValidator(sizeInBytes: 5 * 1024 * 1024,
ErrorMessage = "Image filesize should be smaller than 5 MB")]
[Display(Name = "Profile Photo")]
public IFormFile ProfilePhoto { get; set; }
}
Hope this helps.