2

I have developed an ASP.NET Core Web API which is used to upload files.I am testing the API through postman. When I try to upload file size more than 28 MB it throws request length exceeded error. So I tried to find out solution for this issue. If I increase request length error by changing the value in web.config it works. But my requirement is not to use web.config file. If I increase the MaxRequestBodySize of kestrel server it works fine when I run application through Dotnet CLI but not working when I run application through IIS express from Visual Studio 2019. If I apply [DisableRequestSizeLimit] attribute on POST method it works only on dotnet CLI but not on IIS express. How to increase request length globally for IIS express and Dotnet CLI? Any help is appreciated.

webBuilder.ConfigureKestrel((context, options) =>
                    {
                       
                        options.Limits.MaxRequestBodySize = 524288000;
                    });
user2865466
  • 57
  • 4
  • 8
  • Check out this answer: https://stackoverflow.com/questions/38698350/increase-upload-file-size-in-asp-net-core – Amit Shahani Aug 05 '20 at 12:34
  • I suspect that if you _really_ need this to work in IIS Express - rather than developing using full IIS - then you will need the web.config setting. – stuartd Aug 05 '20 at 12:36
  • Does this answer your question? [Default Limits for IIS Express](https://stackoverflow.com/questions/28155342/default-limits-limits-for-iis-express) – ESG Aug 05 '20 at 12:54

2 Answers2

5

This is the way to configure it globally without changing the web.config :

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
    .UseStartup<Startup>()
    .UseKestrel(options =>
    {
        options.Limits.MaxRequestBodySize = 524288000;
    });

if not Kestrel :

 .UseHttpSys(options =>
 {
    options.MaxRequestBodySize = 524288000;
 });

OR via middelware

        public void Configure(IApplicationBuilder app )
        {
            app.UseWhen(context => context.Request.Path.StartsWithSegments("/api"), appBuilder =>
            {
                context.Features.Get<IHttpMaxRequestBodySizeFeature>().MaxRequestBodySize = null;
            });
        }

if not using UseWhen :

app.Run(async context =>
            {
                context.Features.Get<IHttpMaxRequestBodySizeFeature>().MaxRequestBodySize = 524288000;
            });

OR You could also add header to you controller method :

  [DisableRequestSizeLimit]
  [HttpPost]
  public async Task<IActionResult> Post([FromBody] List<Obj> obj)
  {
  }

//

  [RequestSizeLimit(524288000)]
  [DisableRequestSizeLimit]
  [HttpPost]
   public async Task<IActionResult> Post([FromBody] List<Obj> obj)
   {
   }
mike
  • 1,202
  • 1
  • 7
  • 20
  • 2
    This works when I run the application through Dotnet CLI (kestrel) but not working when i run the application through IIS Express from Visual Studio 2019 – user2865466 Aug 05 '20 at 12:48
  • I updateted my answer, then it's set to unlimited. You can aujust it accordingly – mike Aug 05 '20 at 12:53
  • [RequestSizeLimit(524288000)]- This works only on Kestrel but not on IIS Express – user2865466 Aug 05 '20 at 14:08
  • Kindly check the order of middleware. if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); app.Run(async context => { context.Features.Get().MaxRequestBodySize = 524288000; }); – user2865466 Aug 05 '20 at 14:18
  • 2
    .UseHttpSys - It is not working when i run application through IIS Express from Visual Studio 2019. It throws error as application is running inside iis process but is not configured to use iis server – user2865466 Aug 05 '20 at 14:25
0

When hosting your webapplication in IIS/IIS Express, configure the web.config file accordingly. See this answer

Thomas Luijken
  • 657
  • 5
  • 13