简体   繁体   中英

Reference template as html page using Razor instead of hard coded string

I am using this https://antaris.github.io/RazorEngine/ as a guide.

This code:

using RazorEngine;
using RazorEngine.Templating; // For extension methods.

string template = "Hello @Model.Name, welcome to RazorEngine!";
var result =
    Engine.Razor.RunCompile(template, "templateKey", null, new { Name = "World" });

Instead of using string template ... is it possible to reference it to a index.html file to use as a template?

This is how the HTML file looks like..

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>

    <header class="header" style="text-align: center;">
      <p style="color: red;">Hello @Model.Name</p>
    </header>

  </body>
</html>

A string is a string, regardless of where it came from. You can use File.ReadAllText to read a file as a string. For example:

string template = File.ReadAllText("index.html");
var result = Engine.Razor.RunCompile(template, "templateKey", null, new { Name = "World" });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM