将视图渲染为字符串时超出 MaxJsonLength
好吧,很有趣的问题...
我通过 这篇文章以字符串形式返回部分视图。
这是我的基本控制器的样子:
public abstract class JsonController : Controller
{
protected string RenderPartialViewToString(string viewName, object model)
{
if (string.IsNullOrEmpty(viewName))
viewName = ControllerContext.RouteData.GetRequiredString("action");
ViewData.Model = model;
using (var sw = new StringWriter())
{
var viewEngineResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
var viewContext = new ViewContext(ControllerContext, viewEngineResult.View, ViewData, TempData, sw);
viewEngineResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
}
以下是它的使用方式:
ret = Json(RenderPartialViewToString("_ReportData", viewModel));
但是,现在该视图的结果太大,我收到了可怕的错误:
“字符串的长度超过了 maxJsonLength 属性上设置的值“。
我不知道如何解决这个问题。显然,默认的 JavaScriptSerializer 不遵循 web.config 中的设置,正如许多文档所述。
但是,尝试诸如 LargeJsonResult 类之类的自定义解决方案只会导致字符串被序列化两次< /strong>,这不是我需要的(而且肯定不起作用)。所以,我很茫然。当然,必须有一种方法来处理这个问题,这样我就可以将部分视图作为字符串返回,并指定 maxJsonLength ...有什么建议吗?
是的,我知道这里更大的问题是向客户端发送回太多数据;这个问题将在未来得到解决。这是我现在需要解决的问题。谢谢。
更新 - 我的完整解决方案
感谢 Sergey,我成功了。如果其他人想这样做,请按照以下步骤操作。
第一件事是构建指定的 LargeJsonResult 类 此处。
然后,我利用我的基本控制器类来调用 RenderPartialViewToString,可以找到 此处。
接下来是代码:
var serializer = new JavaScriptSerializer(); Serializer.MaxJsonLength = Int32.MaxValue; var viewString = RenderPartialViewToString("_ReportData", viewModel);
ret = new LargeJsonResult()
{
Data = new
{
result = "Success",
html = viewString
}
};
但仍然需要进行一项调整,那就是修复 JQuery 回调以获取返回的 html 部分。回调函数最初看起来像这样:
function GetReportComplete(response, status, xhr) { ... $('#Report').html(响应);
但需要调整为如下所示:
function GetReportComplete(response, status, xhr) { ... $('#Report').html($.parseJSON(response).html);
有了这个,它就起作用了。
Okay, so interesting problem...
I am returning a Partial View as a string via this post.
Here's what my base Controller looks like:
public abstract class JsonController : Controller
{
protected string RenderPartialViewToString(string viewName, object model)
{
if (string.IsNullOrEmpty(viewName))
viewName = ControllerContext.RouteData.GetRequiredString("action");
ViewData.Model = model;
using (var sw = new StringWriter())
{
var viewEngineResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
var viewContext = new ViewContext(ControllerContext, viewEngineResult.View, ViewData, TempData, sw);
viewEngineResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
}
And here's how it's used:
ret = Json(RenderPartialViewToString("_ReportData", viewModel));
However, now the result of that view is too big and I am getting the dreaded error:
"The length of the string exceeds the value set on the maxJsonLength property".
I am not sure how to remedy this. Obviously, the setting in the web.config is NOT obeyed by the default JavaScriptSerializer as much documentation states this.
However, trying custom solutions like the LargeJsonResult class simply results in the string getting serialized twice, which is not what I need (and definitely doesn't work). So, I'm at a loss. Certainly there has to be a way to handle this so I can return the partial view as a string and also specify the maxJsonLength... Any suggestions?
And yes, I know the larger issue here is sending back too much data to the client; that issue will get addressed in the future. This is the problem I need solved now. Thanks.
UPDATE - MY FULL SOLUTION
Thanks to Sergey, I got this working. In case anyone else wants to do it this way, here's the steps.
First thing is to go build out the LargeJsonResult class specified here.
I then utilized my base controller class to call RenderPartialViewToString which can be found here.
Next is the code:
var serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = Int32.MaxValue;
var viewString = RenderPartialViewToString("_ReportData", viewModel);
ret = new LargeJsonResult()
{
Data = new
{
result = "Success",
html = viewString
}
};
But there was still one adjustment to make, and that was fixing the JQuery callback to fetch out the html part of the return. The callback function originally looked like this:
function GetReportComplete(response, status, xhr) {
...
$('#Report').html(response);
But it needed to be adjusted to look like this:
function GetReportComplete(response, status, xhr) {
...
$('#Report').html($.parseJSON(response).html);
And with that, it works.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这在相同的情况下对我有用:
更新:
首先,我使用以下代码来序列化部分视图:
然后,我使用最初编写的代码将此字符串添加到 JSON:
This worked for me in the same situation:
UPDATE:
First I use the following code to serialize a partial view:
Then I use the code I've written initially to add this string to JSON: