我有一个尝试将 an 渲染ActionResult
到字符串的解决方案。我通过传入我自己的来做到这一点,HttpContext
它将输出文本编写器替换为我自己的TextWriter
.
这就是问题所在 - 元素呈现乱序。如果我通过浏览器直接查询来呈现部分视图,它工作正常。如果我通过我的替代文本编写器渲染它,那么剃刀视图中的任何 @Html.Action 元素都会首先渲染,而不管它们在视图中的位置如何。
所以,这是我的 Razor 观点:
@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
}
);
}
如果我直接通过浏览器查询视图,它会以正确的顺序呈现:
- @Model.DebugText
- Item1.Title
- Item1 动作渲染
- Item2.标题
- Item2 动作渲染
如果我将它呈现给我的 TextWriter,它会按以下顺序呈现:
- Item1 动作渲染
- Item2 动作渲染
- @Model.DebugText
- Item1.Title
- Item2.标题
为什么?
这是我替换文本编写器的方式。(我从一个 ASP.NET WebForms 页面调用它,所以已经有一个现有的HttpContext
)
public static class ActionResultExtensions
{
internal class MyResponseWrapper : HttpResponseWrapper
{
private System.IO.TextWriter _textWriter;
public MyResponseWrapper(HttpResponse wrappedResponse, System.IO.TextWriter textWriter)
: base(wrappedResponse)
{
_textWriter = textWriter;
}
public override System.IO.TextWriter Output
{
get { return this._textWriter; }
set { this._textWriter = value; }
}
}
internal class MyHttpContextWrapper : HttpContextWrapper
{
private readonly System.IO.TextWriter _textWriter;
public MyHttpContextWrapper(System.IO.TextWriter textWriter)
: base(HttpContext.Current)
{
this._textWriter = textWriter;
}
public override HttpResponseBase Response
{
get
{
var httpResponse = HttpContext.Current.Response;
return new MyResponseWrapper(httpResponse, this._textWriter);
}
}
}
public static void Render(this System.Web.Mvc.ActionResult result, System.IO.TextWriter textWriter, System.Web.Routing.RouteData routeData, System.Web.Mvc.ControllerBase controllerBase)
{
var httpContextWrapper = new MyHttpContextWrapper(textWriter);
result.ExecuteResult(new System.Web.Mvc.ControllerContext(httpContextWrapper, routeData, controllerBase));
}
}
public static class MvcUtils
{
public static void RenderControllerAction<T>(Func<T, System.Web.Mvc.ActionResult> f, System.IO.TextWriter writer) where T : System.Web.Mvc.ControllerBase, new()
{
var controller = new T();
// We have to initialise the RouteData so that it knows the name of the controller
// This is used to locate the view
var typeName = controller.GetType().Name;
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("(.*)Controller$");
var match = regex.Match(typeName);
if (match.Success)
{
typeName = match.Groups[1].Value;
}
var routeData = new System.Web.Routing.RouteData();
routeData.Values.Add("controller", typeName);
var actionResult = f(controller);
actionResult.Render(writer, routeData, controller);
}
}
然后我最终使用以下代码将其输出到字符串:
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();
我已经为 编写了一个拦截器
TextWriter
,果然,它收到了三个调用Write(string)
- 写入(LayoutItem 1 动作内容)
- 写入(LayoutItem 2 动作内容)
- Write(Model.DebugText 和这两项标题)