3

我有一个尝试将 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 和这两项标题)
4

1 回答 1

3

我大约在 6 个月前经历过这个。目标是使用部分填充 jquery 弹出对话框。

问题是视图引擎想要以自己尴尬的顺序渲染它们......

尝试这个。LMK 如果需要澄清。

    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();
        }
    }
于 2013-03-06T00:11:40.540 回答