首页 > 解决方案 > 增加 $.get 的查询字符串的长度

问题描述

我正在研究 MVC-5,我有使用 jquery get 函数下载 Excel、PDF、DOC 文件的场景。以下是代码片段:-

    var url = '@Html.Raw(@Url.Action("RendertoEXCEL", "Reports", new { ObjSSRSExportTemplates = @Html.Raw(Json.Encode(@ViewBag.ExportObj)) })) ';

    $.get(url, function (res)
    {

        window.location = url;
        $("#ResultConsReport").mLoading('hide');
    });

行动级别代码如下:-

 public ActionResult RendertoEXCEL(string ObjSSRSExportTemplates)
        {
            byte[] ReadRequest = null;
            JavaScriptSerializer js = new JavaScriptSerializer();
            ExportReportConfig objExport = js.Deserialize<ExportReportConfig>(ObjSSRSExportTemplates);
            string RptFomrat = "EXCEL";
            SSRSReportManager rptManager = new SSRSReportManager(RServiceURL, RptUserName, RptPassword);
            ReadRequest = rptManager.ExportReport(RDirName, objExport.ReportName, RptFomrat, objExport.parmaters);

            return File(ReadRequest, "application/vnd.ms-excel", "Report.xls");
        }

它工作得很好,但是当参数长度大小扩展时。它抛出错误:

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

我用谷歌搜索并增加:

maxUrlLength="10999" maxQueryStringLength="2097151"

在网络配置中但无法正常工作。有没有增加查询字符串的最大长度大小的解决方案?

标签: jqueryasp.net-mvc-5getjson

解决方案


您正在尝试以不打算起作用的方式使用查询字符串,这并不意味着在客户端和服务器之间传递大量数据。您要传递多少数据?

在我看来,解决您的问题的最佳解决方案是在服务器端而不是在客户端生成ExportObj,无论它可能是什么。

如果这不可能,请将您的更改$.get为 a$.post并在帖子正文中而不是在查询字符串中传递对象。像这样:

var url = '@Url.Action("RendertoEXCEL", "Reports")';
var data = { 
    ObjSSRSExportTemplates = @Html.Raw(Json.Encode(ViewBag.ExportObj))
};

$.post(url, data, function (res) {
    // logic
});

推荐阅读