asp.net-mvc ASP.NET MVC 中的文件大小上传限制:web.config(s) 中超过 1 个 maxRequestLength 设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1038742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:40:58  来源:igfitidea点击:

file size upload limitation in ASP.NET MVC: more than 1 maxRequestLength setting in web.config(s)

asp.net-mvcfile-uploadweb-configmaxrequestlength

提问by Alex42

I'd like to have more than 1 setting for maxRequestLength - file size upload limitation (e.g. one for File/New, other for Picture/New). All of my Actions take additional parameters (e.g. /File/New?folderId=234).

我想为 maxRequestLength 设置 1 个以上 - 文件大小上传限制(例如,一个用于文件/新建,另一个用于图片/新建)。我的所有操作都采用附加参数(例如 /File/New?folderId=234)。

Single setting works as expected:

单一设置按预期工作:

<httpRuntime executionTimeout="60" maxRequestLength="1024" />

I tried to have 2 settings with 2 location sections in the root web.config, but without any success. I'm not sure what to write in "path" - physical aspx page of a view, or controller+action... however, nothing seems to work.

我试图在根 web.config 中有 2 个位置部分的 2 个设置,但没有任何成功。我不确定在“路径”中写什么 - 视图的物理 aspx 页面,或控制器 + 操作......但是,似乎没有任何效果。

<location path="/File/">
    <system.web>
        <httpRuntime executionTimeout="60" maxRequestLength="4096" />
    </system.web>
</location>
<location path="/Picture/">
    <system.web>
        <httpRuntime executionTimeout="60" maxRequestLength="1024" />
    </system.web>
</location>

I tried to put another web.config in a specific view folder (e.g. /Views/Picture/...), like it works in classic Webform ASP.NET, but this doesn't seem to do the trick either...

我试图将另一个 web.config 放在特定的视图文件夹中(例如 /Views/Picture/...),就像它在经典的 Webform ASP.NET 中工作一样,但这似乎也不起作用......

<location path="">
    <system.web>
        <httpRuntime executionTimeout="60" maxRequestLength="1024" />
    </system.web>
</location>

No matter what I do, only one value for httpRuntime.maxRequestLength is applied - that in (root) web.config...system.web.

无论我做什么,都只应用了 httpRuntime.maxRequestLength 的一个值 - 在(根)web.config...system.web 中。

回答by Zhaph - Ben Duguid

I believe the Path attribute shouldn't start or end with a "/" - so you should have:

我相信 Path 属性不应该以“/”开头或结尾——所以你应该有:

<location path="File">
  <system.web>
    <httpRuntime executionTimeout="60" maxRequestLength="4096" />
  </system.web>
</location>
<location path="Picture">
  <system.web>
    <httpRuntime executionTimeout="60" maxRequestLength="1024" />
  </system.web>
</location>

Your virtual or physical directory–level Web.config's shouldn't have the <location> elements.

您的虚拟或物理目录级别的 Web.config 不应包含 <location> 元素。

That should sort you out.

那应该可以解决您的问题。

The docs for the Location elementeven have this very example:

Location 元素的文档甚至有这个例子:

The following code example demonstrates how to set the uploaded file size limit to 128 KB for only the page specified.

以下代码示例演示了如何仅将指定页面的上传文件大小限制设置为 128 KB。

<configuration>
  <location path="UploadPage.aspx">
    <system.web>
      <httpRuntime maxRequestLength="128"/>
    </system.web>
  </location>
</configuration>