C# 将ActionResult呈现为字符串-以错误的顺序出现
我尝试将C# 将ActionResult呈现为字符串-以错误的顺序出现,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我尝试将ActionResult呈现为字符串。我通过传入我自己的HttpContext来实现这一点,它用我自己的TextWriter替换输出文本编写器 问题出在这里——元素的渲染顺序不正确。如果通过浏览器直接查询局部视图来渲染局部视图,则效果良好。如果通过替换的文本编写器进行渲染,则razor视图中的任何@Html.Action元素都会首先进行渲染,而不管它们在视图中的位置如何 下面是我的剃须刀观点: @inherits System.Web.Mvc.WebViewPage<Website
ActionResult
呈现为字符串。我通过传入我自己的HttpContext
来实现这一点,它用我自己的TextWriter
替换输出文本编写器
问题出在这里——元素的渲染顺序不正确。如果通过浏览器直接查询局部视图来渲染局部视图,则效果良好。如果通过替换的文本编写器进行渲染,则razor视图中的任何@Html.Action元素都会首先进行渲染,而不管它们在视图中的位置如何
下面是我的剃须刀观点:
@inherits System.Web.Mvc.WebViewPage<WebsitePresentationLayer.MgrScreenLayoutViewer>
@using System.Web.Mvc.Html;
<div>
@Model.DebugText
</div>
@foreach (var item in @Model.Items)
{
<div>@item.Title</div>
@Html.Action(
"LayoutItem",
new
{
id = item.Id,
uniqueName = item.UniqueName
}
);
}
最后,我使用以下代码将其输出为字符串:
StringBuilder sb = new StringBuilder();
StringWriter stringWriter = new StringWriter(sb);
CMS.Website.MvcUtils.RenderControllerAction<PlayerGroupController>
(
c => c.ScreenLayout(this.MgrPlayerGroupViewer.ScreenLayoutId),
stringWriter
);
stringWriter.Flush();
var generatedString = sb.ToString();
StringBuilder sb=新建StringBuilder();
StringWriter StringWriter=新的StringWriter(sb);
CMS.Website.MvcUtils.rendercontrol
(
c=>c.ScreenLayout(this.MgrPlayerGroupViewer.ScreenLayoutId),
编剧
);
stringWriter.Flush();
var generatedString=sb.ToString();
我已经为
TextWriter
编写了一个拦截器,果然,它收到了三个调用Write(string)
- 写入(布局项目1动作内容)
- 写入(布局项目2动作内容)
- 编写(Model.DebugText和两项标题)
public static string RenderPartialViewToString(Controller thisController, string viewName, object model)
{
// assign the model of the controller from which this method was called to the instance of the passed controller (a new instance, by the way)
thisController.ViewData.Model = model;
// initialize a string builder
using (StringWriter sw = new StringWriter())
{
// find and load the view or partial view, pass it through the controller factory
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(thisController.ControllerContext, viewName);
ViewContext viewContext = new ViewContext(thisController.ControllerContext, viewResult.View, thisController.ViewData, thisController.TempData, sw);
// render it
viewResult.View.Render(viewContext, sw);
//return the razorized view/partial-view as a string
return sw.ToString();
}
}
谢谢我必须手动创建自己的ControllerContext,但这解决了我的特殊问题。我仍然希望弄清楚为什么调用ActionResult.ExecuteSult时,ViewEngine会以奇怪的顺序渲染。@AndrewShepherd我记得我在这方面做过努力。起初,我只是试图返回一个部分,但没有得到我想要的东西。我必须通过控制器“处理”我的视图。一旦我决定了方向,整天都要先俯首称臣。你似乎在做类似的事情,但方向略有不同。我必须再次深入了解它。
public static string RenderPartialViewToString(Controller thisController, string viewName, object model)
{
// assign the model of the controller from which this method was called to the instance of the passed controller (a new instance, by the way)
thisController.ViewData.Model = model;
// initialize a string builder
using (StringWriter sw = new StringWriter())
{
// find and load the view or partial view, pass it through the controller factory
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(thisController.ControllerContext, viewName);
ViewContext viewContext = new ViewContext(thisController.ControllerContext, viewResult.View, thisController.ViewData, thisController.TempData, sw);
// render it
viewResult.View.Render(viewContext, sw);
//return the razorized view/partial-view as a string
return sw.ToString();
}
}