通过自定义taghelper渲染部分视图

时间:2016-02-09 17:41:35

标签: c# razor asp.net-core asp.net-core-mvc

我到处搜索,我想要的基本上是MVC6的RenderViewToString,它不使用控制器或动作的上下文。

由于自定义TagHelpers进入他们自己的类,因此这些上下文不可用。

目前我有以下代码,我非常希望将html放在Razor View中,因为这是不可接受的大声笑。

public class WindowTagHelper : TagHelper
{
    public string Title { get; set; }
    public async override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
    {
        var preContent = await output.GetChildContentAsync();      
        var id = context.UniqueId;
        output.TagName = "div";
        output.Attributes["id"] = id;
        output.Attributes["class"] = "window";
        output.TagMode = TagMode.StartTagAndEndTag;

        var windowControls = new StringBuilder();
        windowControls.Append("<div class=\"window-controls\">");
        windowControls.Append("<ul class=\"window-icon-list\">");  

        var minimizeIcon = "<li class=\"control-icon\"><a data-window-id=\"{0}\" class=\"control-icon-minimize\" href=\"#\" data-toggle=\"tooltip\" title=\"Minimize Window\"><div class=\"fa fa-minus\"></div></a></li>".InjectWith(id);
        var maximizeIcon = "<li class=\"control-icon\"><a data-window-id=\"{0}\" class=\"control-icon-maximize\" href=\"#\" data-toggle=\"tooltip\" title=\"Maximize Window\"><div class=\"fa fa-square-o\"></div></a></li>".InjectWith(id);
        var closeIcon = "<li class=\"control-icon\"><a data-window-id=\"{0}\" class=\"control-icon-close\" href=\"#\" data-toggle=\"tooltip\" title=\"Close Window\"><div class=\"fa fa-close\"></div></a></li>".InjectWith(id);

        windowControls.Append(minimizeIcon);
        windowControls.Append(maximizeIcon);
        windowControls.Append(closeIcon);
        windowControls.Append("</ul>");
        windowControls.Append("</div>");

        var content = new StringBuilder();
        content.AppendFormat("<div class=\"window-title\">{0}{1}</div>", windowControls.ToString(), Title);
        content.Append(preContent.GetContent());
        output.Content.AppendHtml(content.ToString());          
    }
}

以下方法不起作用,它会抛出异常并注入ICompositeViewEngine,因为服务需要控制器/操作上下文。

public string RenderPartialViewToString(string viewName, object model)
{
    if (string.IsNullOrEmpty(viewName))
        viewName = ActionContext.ActionDescriptor.Name;

    ViewData.Model = model;

    using (StringWriter sw = new StringWriter())
    {
        var engine = Resolver.GetService(typeof(ICompositeViewEngine)) as ICompositeViewEngine;
        ViewEngineResult viewResult = engine.FindPartialView(ActionContext, viewName);

        ViewContext viewContext = new ViewContext(ActionContext, viewResult.View, ViewData, TempData, sw,new HtmlHelperOptions());

        var t = viewResult.View.RenderAsync(viewContext);
        t.Wait();

        return sw.GetStringBuilder().ToString();
    }
}

1 个答案:

答案 0 :(得分:4)

我们使用自定义标记器和ViewContext的扩展

实现了它
  [ViewContext]
  public ViewContext ViewContext { get; set; }

  public async override void Process(TagHelperContext context, TagHelperOutput output)
  {
     var sw = new StringWriter();

     // Create a new viewData (viewbag). This will be used in a new ViewContext to define the model we want
     ViewDataDictionary viewData = new ViewDataDictionary(ViewContext.ViewData, For.Model);

     // Generate a viewContext with our viewData
     var viewContext = new ViewContext(ViewContext, ViewContext.View, viewData, ViewContext.TempData, sw, new HtmlHelperOptions());

     // Use the viewContext to run the given ViewName
     output.Content.Append(new HtmlString(await viewContext.RenderPartialView(ViewName)));

  }


  public async static Task<string> RenderPartialView(this ViewContext context, string viewName, ICompositeViewEngine viewEngine = null, ViewEngineResult viewResult = null)
  {
     viewEngine = viewEngine ?? context.HttpContext.RequestServices.GetRequiredService<ICompositeViewEngine>();

     viewResult = viewResult ?? viewEngine.FindPartialView(context, viewName);

     await viewResult.View.RenderAsync(context);

     return context.Writer.ToString();

  }