Skip to content

Commit 08eca89

Browse files
committed
feat: Add FilesController to demonstrate how to change request body size on MVC level
1 parent dddd416 commit 08eca89

4 files changed

Lines changed: 31 additions & 7 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Microsoft.AspNetCore.Http;
2+
using Microsoft.AspNetCore.Mvc;
3+
using System.Threading.Tasks;
4+
5+
namespace ReferenceProject.Controllers
6+
{
7+
/// <summary>
8+
/// An example of a controller to upload files to demonstrate the request size limit setting (https://www.talkingdotnet.com/how-to-increase-file-upload-size-asp-net-core/)
9+
/// </summary>
10+
[Route("[controller]")]
11+
[ApiController]
12+
public class FilesController: ControllerBase
13+
{
14+
[HttpPost]
15+
[Route("upload")]
16+
[RequestSizeLimit(1048576)]
17+
// This is not recommended
18+
// [DisableRequestSizeLimit]
19+
public Task<IActionResult> Upload(IFormFile file)
20+
{
21+
return Task.FromResult<IActionResult>(Ok($"Uploaded: {file.FileName}"));
22+
}
23+
}
24+
}

ProjectTemplates/AspNetCore.WebApi/ReferenceProject/ReferenceProject/Controllers/ProductsController.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using AutoMapper;
22
using Microsoft.AspNetCore.Mvc;
3-
using ReferenceProject.Exceptions;
43
using ReferenceProject.Repo;
54
using System;
65
using System.Collections.Generic;

ProjectTemplates/AspNetCore.WebApi/ReferenceProject/ReferenceProject/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void ConfigureServices(IServiceCollection services)
6666
.AddScoped<IUrlHelper>(x => x
6767
.GetRequiredService<IUrlHelperFactory>()
6868
.GetUrlHelper(x.GetRequiredService<IActionContextAccessor>().ActionContext))
69-
.AddMvc(options =>
69+
.AddMvcCore(options =>
7070
{
7171
options.Filters.Add(new ValidateModelFilter());
7272
options.Filters.Add(new CacheControlFilter());

ProjectTemplates/AspNetCore.WebApi/ReferenceProject/ReferenceProject/web.config

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<configuration>
33
<location path="." inheritInChildApplications="false">
4+
<!-- This settings must be used along with requestLimits settings below -->
45
<system.web>
56
<httpRuntime maxRequestLength="1048576" executionTimeout="900" maxUrlLength="4096" maxQueryStringLength="2048" />
67
</system.web>
@@ -9,7 +10,7 @@
910
<modules runAllManagedModulesForAllRequests="false">
1011
<remove name="WebDAVModule" />
1112
</modules>
12-
13+
1314
<handlers>
1415
<clear/>
1516
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
@@ -27,12 +28,12 @@
2728
<security>
2829
<requestFiltering>
2930
<!-- maxAllowedContentLength - Specifies the maximum length of content in a request, in bytes. The default value is 30000000 (~28.6 MB) and has
30-
been reduced to 1048576 (1 MB). This setting is for IIS while the httpRuntime maxRequestLength setting is
31-
for ASP.NET, you need to set both to the same value or the smaller number wins (See http://stackoverflow.com/questions/6327452/which-gets-priority-maxrequestlength-or-maxallowedcontentlength). -->
31+
been reduced to 1048576 (1 MB). This setting is for IIS while the httpRuntime.maxRequestLength is
32+
for ASP.NET, so you need to set both to the same value otherwise the smaller number wins (See http://stackoverflow.com/questions/6327452/which-gets-priority-maxrequestlength-or-maxallowedcontentlength). -->
3233
<!-- maxQueryString - Specifies the maximum length of the query string, in bytes. The default value is 2048. This setting is for IIS while the
33-
httpRuntime maxQueryStringLength setting is for ASP.NET, you need to set both to the same value. -->
34+
httpRuntime.maxQueryStringLength is for ASP.NET, so you need to set up both to the same value. -->
3435
<!-- maxUrl - Specifies maximum length of the URL, in bytes. The default value is 4096. This setting is for IIS while the
35-
httpRuntime maxUrlLength setting is for ASP.NET, you need to set both to the same value. -->
36+
httpRuntime.maxUrlLength is for ASP.NET, so you need to set both to the same value. -->
3637
<requestLimits maxAllowedContentLength="1048576" maxQueryString="2048" maxUrl="4096" />
3738
</requestFiltering>
3839
</security>

0 commit comments

Comments
 (0)