C
C#2mo ago
JayNic

Request Entity Too Large in Azure Web App Production Environment

I'm encountering an error when I'm using an OnPost handler in a razor page to upload files. In debug: the files upload no problem, but as soon as I push to production and try on the actual site: it gives me this error. I have put the following on my PageModel:
[RequestSizeLimit(100 * 1024 * 1024)]
[RequestFormLimits(MultipartBodyLengthLimit = 100 * 1024 * 1024)]
public class ImagesManagerModel : PageModelBase
{
...
[RequestSizeLimit(100 * 1024 * 1024)]
[RequestFormLimits(MultipartBodyLengthLimit = 100 * 1024 * 1024)]
public class ImagesManagerModel : PageModelBase
{
...
I've also added the following in my program.cs:
builder.WebHost.ConfigureKestrel(opt =>
{
opt.Limits.MaxRequestBodySize = 100 * 1024 * 1024;
});
builder.Services.Configure<FormOptions>(options =>
{
options.MultipartBodyLengthLimit = 100 * 1024 * 1024;
});
builder.WebHost.ConfigureKestrel(opt =>
{
opt.Limits.MaxRequestBodySize = 100 * 1024 * 1024;
});
builder.Services.Configure<FormOptions>(options =>
{
options.MultipartBodyLengthLimit = 100 * 1024 * 1024;
});
and even this in my appsettings.json:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"FormOptions": {
"MultipartBodyLengthLimit": 100000000
}
}
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"FormOptions": {
"MultipartBodyLengthLimit": 100000000
}
}
The app is hosted as an Azure App Service. Is there a portal side thing I need to change? Or am I going about this in the totally wrong way? Thanks
5 Replies
JayNic
JayNicOP2mo ago
to add: this is my handler method
public async Task<JsonResult> OnPostUploadFiles([FromForm] FileUploadRequest pData)
{
if (await AuthHelper.IsAuthed(this) == false)
return new JsonResult(null);


AzureBlobService blobService = new AzureBlobService();
var r = await blobService.UploadFiles(pData.Files, pData.Container, pData.Prefix);

return new JsonResult(r);
}


public class FileUploadRequest
{
public string Container { get; set; }
public string Prefix { get; set; }
public List<IFormFile> Files { get; set; }
}
public async Task<JsonResult> OnPostUploadFiles([FromForm] FileUploadRequest pData)
{
if (await AuthHelper.IsAuthed(this) == false)
return new JsonResult(null);


AzureBlobService blobService = new AzureBlobService();
var r = await blobService.UploadFiles(pData.Files, pData.Container, pData.Prefix);

return new JsonResult(r);
}


public class FileUploadRequest
{
public string Container { get; set; }
public string Prefix { get; set; }
public List<IFormFile> Files { get; set; }
}
Mayor McCheese
Mayor McCheese2mo ago
There's a bunch of places this needs to be set. I'd check the docs, overall larger file uploads are a vulnerability, that's why the settings are there. Instead on the upload request you can mint a sas token, send that back to the client, client uses the limited scope token to upload large files with the sdk
JayNic
JayNicOP2mo ago
Thanks for the reply. I ended up finally getting it to work by adding a web.config with the following:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="104857600" /><!--100MB-->
</requestFiltering>
</security>
</system.webServer>



</configuration>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="104857600" /><!--100MB-->
</requestFiltering>
</security>
</system.webServer>



</configuration>
this works. I'm not really sure about sas tokens. I got turned off once it started talking about entra id and all that. Do you know of any good tutorials?
MODiX
MODiX2mo ago
If you have no further questions, please use /close to mark the forum thread as answered
Mayor McCheese
Mayor McCheese2mo ago
I don't, it's well documented, I'm sure you can find an example without looking too hard. Just make sure sas tokens are as tightly scooped as possible and short lived.

Did you find this page helpful?