gpt4 book ai didi

asp.net-mvc - PartialView 作为字符串 + JsonResult

转载 作者:行者123 更新时间:2023-12-04 07:01:39 24 4
gpt4 key购买 nike

我只是在寻找一种很好的方法,如何从服务器返回 JSON 对象,该对象包含部分 View 的 html 和一些额外的数据(如果需要)。

我正在使用 approach by Tim Scott将局部 View 呈现为字符串(进行一些修改 - 使其成为 View 引擎集合感知,使其了解 View 和局部 View 之间的区别,将其放入 StringResult 类并使用 Controller 扩展方法包裹)。

以下是导致问题的原因:

public static string RenderViewToString(ControllerContext controllerContext,
IView view, ViewDataDictionary viewData, TempDataDictionary tempData)
{
Stream filter = null;
ViewPage viewPage =
new ViewPage
{
ViewContext = new ViewContext
(controllerContext, view, viewData, tempData)
};
//Right, create our view

//Get the response context, flush it and get the response filter.
var response = viewPage.ViewContext.HttpContext.Response;
response.Flush();
var oldFilter = response.Filter;

try {
//Put a new filter into the response
filter = new MemoryStream();
response.Filter = filter;

//Now render the view into the memorystream and flush the response
viewPage.ViewContext.View.Render(viewPage.ViewContext, viewPage.ViewContext.HttpContext.Response.Output);
response.Flush();

//Now read the rendered view.
filter.Position = 0;
var reader = new StreamReader(filter, response.ContentEncoding);
return reader.ReadToEnd();
}
finally {
//Clean up.
if (filter != null) {
filter.Dispose();
}

//Now replace the response filter
response.Filter = oldFilter;
}
}

用法如下所示:
var v = this.ViewResultToString(PartialView("_Foo", foo));
return Json(new {Html = Server.HtmlEncode(v), Bar = foo.Bar});

但是 - 这会引发异常:

Server cannot set content type after HTTP headers have been sent.



这是一个堆栈跟踪:

[HttpException (0x80004005): Server cannot set content type after HTTP headers have been sent.] System.Web.HttpResponse.set_ContentType(String value) +8760264 System.Web.HttpResponseWrapper.set_ContentType(String value) +11 System.Web.Mvc.JsonResult.ExecuteResult(ControllerContext context) +131 System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +10 System.Web.Mvc.<>c__DisplayClass11.b__e() +20 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func1 continuation) +255
System.Web.Mvc.<>c__DisplayClass13.<InvokeActionResultWithFilters>b__10() +20
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList
1 filters, ActionResult actionResult) +179 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +395 System.Web.Mvc.Controller.ExecuteCore() +123 System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +23 Company.UI.Project.Controllers.Base.BaseController.Execute(RequestContext requestContext) in c:\Project\Controllers\Base\BaseController.cs:109 System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7 System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +144 System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) +54 System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) +7 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75



事情是 - 每当我调用 RenderViewToString 时,如果我要返回不是 ContentResult 的任何其他 ActionResult,就会抛出异常。

那么 - 有没有另一种好方法如何在没有 3rd 方工具的情况下将匿名对象序列化为 Json

如何使这种方法发挥作用(如何杀死那个该死的异常,
究竟是什么强制发送这些 header )?

最佳答案

使用 this 让它工作起来.

这就是问题所在:

In the first case, intercepting the output to HttpResponse using a "capturing filter" forces you to flush the output before the whole view is rendered and, since the original HttpResponse object is used, doesn't allow you to change content encoding, mime type or add headers after the partial view has been rendered.



解决方案 - 不要使用“刷新”技术来呈现部分 View 。

关于asp.net-mvc - PartialView 作为字符串 + JsonResult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1748428/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com