I 使用Formatwith nuget解析出object的属性值
1. install package FormatWith
2. Add a View to check the result
@model string
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Home</title>
</head>
<body>
<div>
@Model
</div>
</body>
</html>
3. Controller
public ActionResult FormatWithTest()
{
string emailContent = "{CurrentTime} - {Name}".FormatWith(
new
{
CurrentTime = DateTime.Now, Name = "ddd"
});
return View("Result", model: emailContent);
}
II 使用razor engine+自定义view解析出HTML
1. install package razorengine
2. template view
@model emailResearch.Controllers.EmailVm
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>@Model.Title</title>
</head>
<body>
<div>
@Model.Content
</div>
</body>
</html>
3. email view model
public class EmailVm
{
public string Title { get; set; }
public string Content { get; set; }
}
4. usage
public ActionResult FormatUseRazor()
{
var viewModel = new EmailVm()
{
Title = "ABC",
Content = "aaaaa"
};
string template = System.IO.File.ReadAllText(HttpContext.Server.MapPath("~/Views/Hello" +
"/EmailTemplate.cshtml"));
var body = Razor.Parse(template, viewModel);
return View("Result", model: body);
}