C# 如何配置 web.config 以允许任何长度的请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11636386/
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-08-09 18:36:40  来源:igfitidea点击:

How to configure the web.config to allow requests of any length

c#javascriptasp.net-mvcweb-configquery-string

提问by some_bloody_fool

I am building a site in which i would like to create a file client side from the value of a textarea element.

我正在构建一个站点,我想在其中根据 textarea 元素的值创建文件客户端。

I have the code in place to do this, but i am getting this error

我有执行此操作的代码,但出现此错误

HTTP Error 404.15 - Not Found The request filtering module is configured to deny a request where the query string is too long.

HTTP 错误 404.15 - Not Found 请求过滤模块配置为拒绝查询字符串过长的请求。

Is there a way to override this so that I am able to process requests of any size?

有没有办法覆盖它,以便我能够处理任何大小的请求?

If not, is there a way to generate files client side without using the filesystem/active x object?

如果没有,有没有办法在不使用文件系统/活动 x 对象的情况下生成文件客户端?

thanks

谢谢

采纳答案by Matt Varblow

Add the following to your web.config:

将以下内容添加到您的 web.config:

<system.webServer>
  <security>
    <requestFiltering>
      <requestLimits maxQueryString="32768"/>
    </requestFiltering>
  </security>
</system.webServer>

See:

看:

http://www.iis.net/ConfigReference/system.webServer/security/requestFiltering/requestLimits

http://www.iis.net/ConfigReference/system.webServer/security/requestFiltering/requestLimits

Updated to reflect comments.

更新以反映评论。

requestLimits Element for requestFiltering [IIS Settings Schema]

requestLimits 元素用于 requestFiltering [IIS 设置架构]

You may have to add the following in your web.config as well

您可能还需要在 web.config 中添加以下内容

<system.web>
    <httpRuntime maxQueryStringLength="32768" maxUrlLength="65536"/>
</system.web>

See: httpRuntime Element (ASP.NET Settings Schema)

请参阅:httpRuntime 元素(ASP.NET 设置架构)

Of course the numbers (32768 and 65536) in the config settings above are just examples. You don't have to use those exact values.

当然,上面配置设置中的数字(32768 和 65536)只是示例。您不必使用这些确切的值。

回答by Leniel Maccaferri

In my case ( Visual Studio 2012 / IIS Express / ASP.NET MVC 4 app / .Net Framework 4.5 ) what really worked after 30 minutes of trial and error was setting the maxQueryStringLengthproperty in the <httpRuntime>tag:

就我而言(Visual Studio 2012 / IIS Express / ASP.NET MVC 4 app / .Net Framework 4.5)经过 30 分钟的反复试验,真正起作用的是maxQueryStringLength<httpRuntime>标签中设置属性:

<httpRuntime targetFramework="4.5" maxQueryStringLength="10240" enable="true" />

maxQueryStringLengthdefaults to 2048.

maxQueryStringLength默认为2048.

More about it here:

更多关于它的信息:

Expanding the Range of Allowable URLs

扩大允许的 URL 范围



I tried setting it in <system.webServer>as @MattVarblowsuggests, but it didn't work... and this is because I'm using IIS Express (based on IIS 8) on my dev machine with Windows 8.

我尝试<system.webServer>按照@MattVarblow 的建议进行设置,但没有奏效……这是因为我在装有 Windows 8 的开发机器上使用 IIS Express(基于 IIS 8)。

When I deployed my app to the production environment (Windows Server 2008 R2 with IIS 7), IE 10 started returning 404 errors in AJAX requests with long query strings. Then I thought that the problem was related to the query string and tried @MattVarblow's answer. It just worked on IIS 7. :)

当我将我的应用程序部署到生产环境(带有 IIS 7 的 Windows Server 2008 R2)时,IE 10 开始在带有长查询字符串的 AJAX 请求中返回 404 错误。然后我认为问题与查询字符串有关,并尝试了@MattVarblow 的回答。它只适用于 IIS 7。:)

回答by user3635095

HTTP Error 404.15 - Not Found The request filtering module is configured to deny a request where the query string is too long.

HTTP 错误 404.15 - Not Found 请求过滤模块配置为拒绝查询字符串过长的请求。

To resolve this problem, check in the source code whether the Formtag has a property methodis get/set state.

要解决此问题,请在源代码中检查Form标签是否具有methodget/set 状态的属性。

If so, the methodproperty should be removed.

如果是这样,method则应删除该属性。

回答by Ronald Nsabiyera

I had a similar issue trying to deploy an ASP Web Application to IIS 8. To fix it I did as Matt and Leniel suggested above. But also had to configure the Authentication setting of my site to enable Anonymous Authentication. And that Worked for me.

我在尝试将 ASP Web 应用程序部署到 IIS 8 时遇到了类似的问题。为了修复它,我按照上面的 Matt 和 Leniel 的建议做了。但还必须配置我站点的身份验证设置以启用匿名身份验证。这对我有用。

回答by SteveCav

Something else to check: if your site is using MVC, this can happen if you added [Authorize] to your login controller class. It can't access the login method because it's not authorized so it redirects to the login method --> boom.

要检查的其他事项:如果您的站点使用 MVC,如果您将 [Authorize] 添加到您的登录控制器类,则可能会发生这种情况。它无法访问登录方法,因为它没有被授权,所以它重定向到登录方法 --> 繁荣。

回答by Andrew Gale

I had to add [AllowAnonymous] to the ActionResult functions in my login page because the user was not authenticated yet.

我必须将 [AllowAnonymous] 添加到登录页面的 ActionResult 函数中,因为用户尚未通过身份验证。

回答by Steve Smith

If your website is using authentication, but you don't have the correct authentication method set up in IIS (e.g. Basic, Forms etc..) then the browser will be getting stuck in a redirect loop. This causes the redirect url to get longer and longer until it explodes.

如果您的网站使用身份验证,但您没有在 IIS 中设置正确的身份验证方法(例如 Basic、Forms 等),那么浏览器将陷入重定向循环。这会导致重定向 url 变得越来越长,直到它爆炸。

回答by Arvo Bowen

If you run into this issue when running an IIS 8.5 web server you can use the following method.

如果您在运行 IIS 8.5 Web 服务器时遇到此问题,您可以使用以下方法。

First, find the "Request Filtering"module in the IIS site you are working on, then double click it...

首先,在您正在处理的IIS站点中找到“请求过滤”模块,然后双击它...

enter image description here

在此处输入图片说明

Next, you need to right click in the white area shown below then click the context menu option called "Edit Feature Settings".

接下来,您需要右键单击下面显示的白色区域,然后单击名为“编辑功能设置”的上下文菜单选项。

enter image description here

在此处输入图片说明

Then the last thing to do is change the "Maximum query string (Bytes)"value from 2048to something more appropriate such as 5000for your needs.

然后最后要做的是将“最大查询字符串(字节)”值从2048更改为更合适的值,例如5000以满足您的需要。

enter image description here

在此处输入图片说明

回答by rinku Choudhary

It will also generate error when you pass large string in ajax call parameter.

当您在 ajax 调用参数中传递大字符串时,它也会产生错误。

so for that alway use type post in ajax will resolve your issue 100%and no need to set the length in web.config.

因此,始终在 ajax 中使用类型 post 将100%解决您的问题,无需在 web.config 中设置长度。

// var UserId= array of 1000 userids

// var UserId= 1000 个用户 ID 的数组

$.ajax({ global: false, url: SitePath + "/User/getAussizzMembersData", "data": { UserIds: UserId}, "type": "POST", "dataType": "JSON" }}

$.ajax({ global: false, url: SitePath + "/User/getAussizzMembersData", "data": { UserIds: UserId}, "type": "POST", "dataType": "JSON" }}