Mohammad Taherian
Mohammad Taherian

Reputation: 1694

Define different APIs to accept different size limits in ASP.Net Core 2.2

I have a controller which contains different methods. How can I define specific request size limit for each method?

I've tried these links, but I don't want to define a global size in web.config or startup

Asp.net Core 2.0 RequestSizeLimit attribute not working

Asp.net Core RequestSizeLimit still executes action

HTTP Error 404.13 - asp.net core 2.0

Increase upload file size in Asp.Net core

[HttpPost]
[RequestSizeLimit(1000000)]
public void MyMethod1([FromBody] string value)
{
 // Do something
}

[HttpPost]
[RequestSizeLimit(2000000)]
public void MyMethod2([FromBody] string value)
{
 // Do something else
}

I expect MyMethod1 accepts only requests with size less than 1000000 bytes and MyMethod2 accepts requests with size less than 2000000 bytes. But this does not work and they accept requests with any size. What is the issue here?

Upvotes: 3

Views: 900

Answers (1)

Ramin Azali
Ramin Azali

Reputation: 283

That solution you tested its used for MVC not .Net Core

What I Found mostly people using solution (Global Solution) in the below:

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

but it have different ways for it like MiddleWare like below:

  app.UseWhen(context => context.Request.Path.StartsWithSegments("/api"), 
        appBuilder =>{
        context.Features.Get<IHttpMaxRequestBodySizeFeature> 
        ().MaxRequestBodySize = null;
        //TODO: take next steps
  });

Sorry For My Bad English :)

Source => Helper Link

Upvotes: 1

Related Questions