此请求的查询字符串的长度超过配置的maxQueryStringLength值

时间:2013-07-11 10:01:43

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

我正在尝试重定向到一个视图并继续在问题标题中发布错误。

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

enter image description here

向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;
        }
    }
}

5 个答案:

答案 0 :(得分:13)

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

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

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

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

答案 1 :(得分:4)

为什么不使用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 ...
}

答案 2 :(得分:1)

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

答案 3 :(得分:1)

在你的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

答案 4 :(得分:0)

我修正了以下问题: 运行正常

<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>

web.config