【问题标题】:The length of the query string for this request exceeds the configured maxQueryStringLength value此请求的查询字符串长度超过配置的 maxQueryStringLength 值
【发布时间】:2013-07-09 13:19:28
【问题描述】:

我正在尝试重定向到一个视图并不断收到问题标题中发布的错误。

在断点测试期间,通过代码 iv 的第一位的代码位于设置消息和设置异常的下方。返回重定向后继续后显示的下一页如下。

在 ErrorController 和错误模型中添加断点我发现代码永远不会到达那里。

我试图发布到的视图是一个错误页面。这是帮助您查看问题的代码。

RedirectToAction:

string message;
message = "An error has occured during the communication to lightstone, this is likely a timeout issue and could be the result of a bad connection. Please go back and try again.";
return RedirectToAction("Error", "Error", new { ex = ex.ToString(), message =  message});

我的 ErrorController 中的操作:

public ActionResult Error(string ex, string message)
{
   ViewBag.Message = "Error";
   return View(new ErrorModel(ex, message));
}

我的错误模型:

namespace MvcResComm.Models
{
    public class ErrorModel
    {
        public string ex { get; set; }
        public string message { get; set; }

        public ErrorModel(string ex, string message)
        {
            this.ex = ex;
            this.message = message;
        }
    }
}

【问题讨论】:

标签: c# asp.net-mvc-3


【解决方案1】:

在 web.config 文件中有一个可设置的最大 URL 长度值。这个问题有类似的问题 ASP.NET MVC, Url Routing: Maximum Path (URL) Length

【讨论】:

  • 错误从重定向到操作发生?为什么 URL 长度会产生影响?
  • 因为 URL 长度是对 GET 请求大小的定义限制。这样做是为了确保支持无法处理超过特定大小的 URL 长度的早期浏览器。当您重定向到一个动作(在控制器上)时,它会对该动作执行 GET
  • 谢谢,我现在明白了,然后我如何将这些字符串从发生错误的一个控制器传递到错误控制器,而不必在 url 中传递它?或者我如何设置最大 url 大小?
【解决方案2】:

你为什么不使用TempData,它是用来做这样的事情的。比如:

TempData["ErrorMessage"] = "An error has occured during the communication to lightstone, this is likely a timeout issue and could be the result of a bad connection. Please go back and try again.";

检查这个link

编辑

像这样传递您的异常消息:

TempData["Error"] = ex.Message();
TempData["ErrorMessage"] = "An error has occured during the communication to lightstone, this is likely a timeout issue and could be the result of a bad connection. Please go back and try again.";

return RedirectToAction("Error", "Error");

然后只需从您的ErrorController 访问它,例如:

public ActionResult Error(string ex, string message)
{
    var error = (string)TempData["Error"];
    // do other magic ...
}

【讨论】:

  • 谢谢,我会确保这样做,但我认为错误是在我得到错误控制器之前。错误从重定向到操作发生。
  • @Pomster 抱歉,我不清楚,请检查我的编辑。我希望它有所帮助。
  • @Pomster,实际上,我没有测试它,但我很确定你可以将它直接传递给视图(我的意思是你可以直接在你的视图中访问TempData["Error"],你不要'不必通过它)。
  • 非常感谢它的工作原理,在操作结果中,只需在参数部分删除(字符串 ex,字符串消息),并使用 TempDate[] 传递大字符串,保留代码相同。非常感谢您的帮助,在其他答案之后,我的错误页面上会有一个巨大的 url,这无疑是更好的方法。
  • @Pomster 很高兴我能帮上忙 :) 干杯
【解决方案3】:

在你的 web.config 中 在<system.web> <httpRuntime> 标签下你可以设置你的maxQueryStringLength

原来如此

<system.web>
  <httpRuntime maxQueryStringLength = "**MY NUMBER**" />
</system.web>

查看 msdn 参考: http://msdn.microsoft.com/en-us/library/e1f13641%28v=vs.100%29.aspx

另外请在 IIS 配置中增加 maxQueryStringLength,查看:

http://www.iis.net/configreference/system.webserver/security/requestfiltering/requestlimits

【讨论】:

  • Iv 将此设置为较大的数字,但什么也没发生?
  • 查看我的编辑。还有一种方法可以从 IIS 中增加 maxQueryStringLength
【解决方案4】:

在您项目的根web.config 中,在system.web 节点下:

<system.web>
    <httpRuntime maxUrlLength="10999" maxQueryStringLength="2097151" />
...

此外,我必须在 system.webServer 节点下添加它,否则我的长查询字符串出现安全错误:

<system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxUrl="10999" maxQueryString="9999" />
      </requestFiltering>
    </security>
...

【讨论】:

  • 我的system.webserver 已经包含&lt;httpRuntime targetFramework="4.0"/&gt;,所以我已将其更新为&lt;httpRuntime targetFramework="4.0" maxUrlLength="10999" maxQueryStringLength="2097151" /&gt;。如果我添加了一个新的httpRuntime 标签,我会收到一个IIS 500.19 错误。
【解决方案5】:

我固定关注: 运行正常

<system.webServer>
    <security>
        <requestFiltering>
            <alwaysAllowedQueryStrings>
                <add queryString="maxQueryString" />
                <add queryString="maxAllowedContentLength" />
                <add queryString="maxUrl" />
            </alwaysAllowedQueryStrings>
            <requestLimits maxUrl="10999" maxQueryString="2097151" />
        </requestFiltering>
    </security>
</system.webServer>

并添加

<system.web>
    <httpRuntime maxUrlLength="10999" maxQueryStringLength="2097151" />
</system.web>

【讨论】:

    猜你喜欢
    • 2013-04-18
    • 1970-01-01
    • 2011-12-30
    • 2014-02-10
    • 2012-09-24
    • 1970-01-01
    • 2012-08-06
    • 2018-04-25
    • 2017-08-31
    相关资源
    最近更新 更多