HTML模板
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>@Model.Title</title> </head> <body> <h1>@Model.Title</h1> <p>作者:@Model.Author - 发布时间:@Model.CreateDate</p> <p>@Raw(Model.Content)</p> </body> </html>
Model 层(ArticleModel.cs)
public class ArticleModel { /// <summary> /// 文章ID /// </summary> public int Id { get; set; } /// <summary> /// 文章标题 /// </summary> public string Title { get; set; } /// <summary> /// 文章内容 /// </summary> public string Content { get; set; } /// <summary> /// 作者 /// </summary> public string Author { get; set; } /// <summary> /// 发布时间 /// </summary> public DateTime CreateDate { get; set; } }
View层(C# 程序)
public void OnGet() { //模板路径 string filePath = "F:\\csharp\\WebApplication1\\WebApplication1\\Templates\\Default\\index.html"; // 读取模板 string template = System.IO.File.ReadAllText(filePath); //添加模板 Engine.Razor.AddTemplate("index.cshtml", template); //编译模板 Engine.Razor.Compile("index.cshtml", null); //执行模板 var str = Engine.Razor.Run("index.cshtml", null, new ArticleModel() { Title = "标题" }); }
标签:set,string,NetCore,get,引擎,Model,public,模板 From: https://www.cnblogs.com/microsoft-zh/p/18292047