【问题标题】:How to get view file content as plain string如何以纯字符串形式获取查看文件内容
【发布时间】:2021-03-21 19:08:14
【问题描述】:

ASP.NET Core 5 MVC 应用程序使用来自 https://github.com/adoconnection/RazorEngineCore 的 razor 引擎在运行时将视图呈现为字符串。

如何获取查看~/Views/Checkout/Order.cshtml内容为字符串?

我试过了

ReadTemplate("~/Views/Checkout/Order.cshtml")

但它会引发错误。应用程序可以部署为单个文件或单独的文件。

string ReadTemplate(string filename)
{
    var stream = GetType().Assembly.GetManifestResourceStream(filename);

    if (stream == null)
        throw new ApplicationException(filename + " view not found");

    StreamReader reader = new(stream);
    string template = reader.ReadToEnd();
    reader.Close();
    return template;
}

【问题讨论】:

  • 想想如果你的 Razor 视图被预编译会发生什么 - 如果 .cshtml 文件未包含在部署中,则无法获取它们的原始源。它们也不包含在程序集资源中。
  • 如何强制将视图作为程序集资源包含在内?它仅由应用程序用作要呈现的字符串内容。它不是常规视图,不用于创建 html 页面。将构建操作设置为embedded resource 是否足够。应该使用哪个路径来获取此资源?
  • 如果不是 Razor 文件,为什么会有.cshtml 文件扩展名?
  • 是剃须刀文件

标签: asp.net-core asp.net-core-mvc razorengine asp.net5


【解决方案1】:

如何以字符串形式获取视图~/Views/Checkout/Order.cshtml 内容?

您可以尝试使用以下代码创建 ViewRenderService:

public interface IViewRenderService
{
    Task<string> RenderToString(string viewName, object model);
}
public class ViewRenderService : IViewRenderService
{
    private readonly IRazorViewEngine _razorViewEngine;
    private readonly ITempDataProvider _tempDataProvider;
    private readonly IHttpContextAccessor _contextAccessor;

    public ViewRenderService(IRazorViewEngine razorViewEngine,
                             ITempDataProvider tempDataProvider,
                             IHttpContextAccessor contextAccessor)
    {
        _razorViewEngine = razorViewEngine;
        _tempDataProvider = tempDataProvider;
        _contextAccessor = contextAccessor;
    }

    public async Task<string> RenderToString(string viewName, object model)
    {
        var actionContext = new ActionContext(_contextAccessor.HttpContext, _contextAccessor.HttpContext.GetRouteData(), new ActionDescriptor());

        await using var sw = new StringWriter();
        var viewResult = FindView(actionContext, viewName);

        if (viewResult == null)
        {
            throw new ArgumentNullException($"{viewName} does not match any available view");
        }

        var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary())
        {
            Model = model
        };

        var viewContext = new ViewContext(
            actionContext,
            viewResult,
            viewDictionary,
            new TempDataDictionary(actionContext.HttpContext, _tempDataProvider),
            sw,
            new HtmlHelperOptions()
        );

        await viewResult.RenderAsync(viewContext);
        return sw.ToString();
    }

    private IView FindView(ActionContext actionContext, string viewName)
    {
        var getViewResult = _razorViewEngine.GetView(executingFilePath: null, viewPath: viewName, isMainPage: true);
        if (getViewResult.Success)
        {
            return getViewResult.View;
        }

        var findViewResult = _razorViewEngine.FindView(actionContext, viewName, isMainPage: true);
        if (findViewResult.Success)
        {
            return findViewResult.View;
        }

        var searchedLocations = getViewResult.SearchedLocations.Concat(findViewResult.SearchedLocations);
        var errorMessage = string.Join(
            Environment.NewLine,
            new[] { $"Unable to find view '{viewName}'. The following locations were searched:" }.Concat(searchedLocations));

        throw new InvalidOperationException(errorMessage);
    }
}

在上述服务中添加以下引用:

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewEngines;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Routing;

然后,在 Startup.ConfigureServices 方法中注册 ViewRenderService:

services.AddScoped<IViewRenderService, ViewRenderService>();

之后,您可以使用此服务将视图页面呈现为字符串:

public class HomeController : Controller
{ 
    private readonly IViewRenderService _viewRenderService;
    public HomeController(IViewRenderService viewRenderService)
    {  
        _viewRenderService = viewRenderService;
    }
     
    public async Task<IActionResult> CategoryIndex()
    {  
       //The model will be transferred to the View page.
        var stulist = new List<Student>() { new Student() { StudentName = "AA" }, new Student() { StudentName = "BB" } };

        var result =  _viewRenderService.RenderToString("Views/Student/Index.cshtml", stulist).Result;

        return View();
    }

结果如下:

【讨论】:

  • 谢谢。很棒的代码。如何将视图内容作为字符串传递,以便视图可以从数据库中读取?应该有两种可能性:将视图作为字符串传递或通过代码中的名称传递
  • 是的,你是对的。在我看来,我更喜欢将视图名称存储在数据库中,然后,从数据库中获取数据后,我还可以选择使用哪个视图来呈现记录(根据视图名称)。
  • 查看内容(不是名称)存储在数据库中。如何使用此内容来呈现视图?它应该在运行时编译。为什么使用.Result 而不是await
  • 嗨@Andrus,获取视图内容(html资源)后,您可以使用@Html.Raw()方法来渲染html内容。 “为什么使用.Result而不是await”,我是用它来调试的,它可以帮助更清楚地看到html内容。
  • 从数据库查看内容包含@inject和其他剃须刀命令。如何强制@Html.Raw从数据库编译和渲染视图?如果在视图中使用异步方法,.Result 的使用会导致错误,因为执行没有暂停?
【解决方案2】:

在您的评论回复中,您写道:

如何强制将视图作为程序集资源包含在内?它仅由应用程序用作要呈现的字符串内容。它不是常规视图,不用于创建 html 页面。将构建操作设置为嵌入式资源是否足够?应该使用哪个路径来获取此资源?

您可以自己infer the resource stream name,也可以通过直接编辑.csproj 文件来指定&lt;LogicalName&gt;you can manually specify a name(我不知道为什么这个元素没有显示在“属性”窗口中,它确实应该是)。后一种选择更好,因为在移动文件时名称将保持不变)。

【讨论】:

    猜你喜欢
    • 2014-04-19
    • 2016-02-20
    • 2013-10-17
    • 1970-01-01
    • 2012-10-06
    • 2015-12-08
    • 2016-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多