how to set the file size limit in razor pages?

mc 3,866 Reputation points

I create UploadImage.cshtml and OnPostUploadImageAsync

the Request.Form.Files is exception:

Multipart body length limit 16384 exceeded.

I set the opt:

builder.Services.AddRazorPages(options =>
options.Conventions.AddPageApplicationModelConvention("/Manage/ImageUpload", x => { x.Filters.Add(new RequestFormLimitsAttribute() { MultipartBodyLengthLimit = 268435456 }); });

and the

builder.Services.Configure<Microsoft.AspNetCore.Http.Features.FormOptions>(x =>

x.ValueLengthLimit = 268435456;
x.MultipartBodyLengthLimit = 268435456;


and [RequestFormLimits(MultipartBodyLengthLimit = 268435456)]

but still the exception.

A set of technologies in the .NET Framework for building web applications and XML web services.
4,282 questions
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,051 Reputation points Microsoft Vendor

    Hi @mc

    I am not hosting iis but hosting dotnet run.

    You are using Kestrel, so you need to configure request body limit on Kestrel, code like this:

    builder.WebHost.ConfigureKestrel(opt =>
        opt.Limits.MaxRequestBodySize=null;  //disable the request body limit.

    Then, configure the FormOptions as below:

    builder.Services.Configure<FormOptions>(options =>
        options.ValueLengthLimit = int.MaxValue;
        options.MultipartBodyLengthLimit = int.MaxValue; 

    In the Razor page Post method, get the upload file from the IFormCollection, instead of using IFormFile and model binding.

        public class FileUploadModel : PageModel
            private IWebHostEnvironment _environment;
            public FileUploadModel(IWebHostEnvironment environment)
                _environment = environment;
            [RequestFormLimits(MultipartBodyLengthLimit = 268435456)] 
            public async Task OnPostAsync([FromForm] IFormCollection formData)
                var file = formData.Files["file"];
                if (file != null && file.Length > 0)
                    var filePath = Path.Combine(_environment.WebRootPath, "uploads", file.FileName);
                    using (var stream = new FileStream(filePath, FileMode.Create))
                        await file.CopyToAsync(stream);

    The Razor page as below: without using model binding, just using an input file element.

    @model RazorWebApp.Pages.FileUploadModel
    <form method="post" enctype="multipart/form-data">
        <input id="file" type="file" name="file" />
        <input type="submit" />

    The result as below: the upload file size 197MB.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,


    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bruce ( 59,046 Reputation points

    If hosting with IIS you must also set its max limit

         <requestLimits maxAllowedContentLength="268435456" />
    1 person found this answer helpful.