Skip to main content
lennartb's blog

Increasing the ASP.NET Core upload limit for a specific endpoint via attribute

By default, ASP.NET Core limits the max request body size to 30 MB. The limit can be increased by using a middleware, or global Kestrel configuration, as described in the linked issue. What happens if you don't want to globally increase the limit, but just for a single endpoint? The same configuration can be packaged into an attribute, and the size limit can even be made dependent of an IConfiguration value.

using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc.Filters;

namespace IncreaseUploadLimitDemo;

[AttributeUsage(AttributeTargets.Method)]
public class UploadSizeLimitFilter(string appSettingsConfigName) : Attribute, IAuthorizationFilter
{
    public void OnAuthorization(AuthorizationFilterContext context)
    {
        var configuration = context.HttpContext.RequestServices.GetService<IConfiguration>();

        if (configuration?.GetValue<long?>(appSettingsConfigName) is not { } maxRequestBodySize) return;

        if (context.HttpContext.Features.Get<IHttpMaxRequestBodySizeFeature>() is { } maxRequestBodySizeFeature)
        {
            maxRequestBodySizeFeature.MaxRequestBodySize = maxRequestBodySize;
        }
    }
}

An IAuthorizationFilter is used because IHttpMaxRequestBodySizeFeature.MaxRequestBodySize can only be set as long as the request has not been read yet, which is the case for a regular IActionFilter for example.

Assuming the configuration value name MaxRequestBodySize has been set in appsettings.json similar to this:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "MaxRequestBodySize": 500000000
}

an endpoint can now be annotated with the previously implemented attribute:

 [HttpGet(Name = "GetWeatherForecast")]
 [UploadSizeLimitFilter("MaxRequestBodySize")]
 public IEnumerable<WeatherForecast> Get()
 {
     return Enumerable.Range(1, 5).Select(index => new WeatherForecast
         {
             Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
             TemperatureC = Random.Shared.Next(-20, 55),
             Summary = Summaries[Random.Shared.Next(Summaries.Length)]
         })
         .ToArray();
 }

Updating the Pi-hole instance running in a container in OpenMediaVault

Since the Pi-hole Docker image should not be upgraded the same way as a physical instance (using pihole -up), the container for each new release has to be rebuild. It's surprisingly hard to find up-to-date information on how to update the image when running Pi-hole on top of an OpenMediaVault instance. Most available information is already a couple of years old, and often refers to using additional plugins (Watchtower) or still applies to the outdated Portainer variant.

OMV Pi-hole update

It's actually pretty simple: First, navigate to "Services"-"Compose"-"Files" in the OMV web UI. Then, selecting the "pull" button gets the latest container version from the registry. This can take a couple of minutes. Afterwards, the container needs to be taken down using the "down" button and started again using "up".

Assuming the container has been configured with volumes to store the user data, no configuration data is lost - it's an inplace upgrade just like using pihole-up.