在 ASP.NET 5 中将 Razor 视图渲染为字符串 [英] Render Razor view to string in ASP.NET 5

查看:38
本文介绍了在 ASP.NET 5 中将 Razor 视图渲染为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以前版本的 ASP.NET 中,虽然不是很简单,但可以将 Razor 视图呈现为字符串.我的方法似乎是使用假控制器,或者也使用一些外部引擎,如 RazorEngine.

In previous versions of ASP.NET it was possible, although not very simple, to render Razor views as strings. The methods I've seem are to use a fake controller, or also to use some external engine like RazorEngine.

现在,ASP.NET 5 改变了很多东西,我想知道这是否比以前更简单.那么在新版本的框架中,是否有一种直接的方法可以将 Razor 视图呈现为字符串,或者我们仍然需要使用以前版本中的方法?

Now, many things are changed with ASP.NET 5 and I was wondering whether or not this is now simpler than before. So in the new version of the framework is there one straightforward way to render Razor views as strings or we still need to use the methods from the previous versions?

推荐答案

我使用了从 IServiceProvider 注入的以下类型:

I use the following types injected from the IServiceProvider:

ICompositeViewEngine viewEngine;
ITempDataProvider tempDataProvider;
IHttpContextAccessor httpContextAccessor;

我使用以下方法呈现内容:

I render the content using the following method:

private async Task<string> RenderView(string path, ViewDataDictionary viewDataDictionary, ActionContext actionContext)
{
    using (var sw = new System.IO.StringWriter())
    {
        var viewResult = viewEngine.FindView(actionContext, path);

        var viewContext = new ViewContext(actionContext, viewResult.View, viewDataDictionary, new TempDataDictionary(httpContextAccessor, tempDataProvider), sw);

        await viewResult.View.RenderAsync(viewContext);
        sw.Flush();

        if (viewContext.ViewData != viewDataDictionary)
        {
            var keys = viewContext.ViewData.Keys.ToArray();
            foreach (var key in keys)
            {
                viewDataDictionary[key] = viewContext.ViewData[key];
            }
        }

        return sw.ToString();
    }
}

我这样称呼它:

var path = "~/Views/Home/Index.cshtml";
var viewDataDictionary = new ViewDataDictionary(new Microsoft.AspNet.Mvc.ModelBinding.EmptyModelMetadataProvider(), new Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary());
var actionContext = new ActionContext(httpContextAccessor.HttpContext, new Microsoft.AspNet.Routing.RouteData(), new ActionDescriptor());
viewDataDictionary.Model = null;
var text = await RenderView(path, viewDataDictionary, actionContext);

当然,我的viewDataDictionaryactionContext 变量是通过另一种方法设置的,用于封装.如果您愿意,对 new ViewDataDictionary 行的修改可能会导致类型化模型绑定到您的视图.

Of course, my viewDataDictionary and actionContext variables are set by another method for encapsulation. A modification to the new ViewDataDictionary line can result in a typed Model being bound to your View if you choose.

这段代码大量使用,我想我已经在下面列出了它们.否则,VS2015 很容易找到它们.

This code uses heavy usings, I think I've listed them below. Otherwise, VS2015 is pretty good about finding them.

using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Rendering;

这是在 beta-3 下编写的;它仍在构建,但有些事情可能会改变.如果有的话,我会试着回到这里更新.

This was written under beta-3; it still builds, but some things may change. I'll try to come back here to update if it does.

这篇关于在 ASP.NET 5 中将 Razor 视图渲染为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆