ASP.NET MVC中 使用邮件模板的两种方式

I 使用Formatwith nuget解析出object的属性值


1. install package FormatWith


2. Add a View to check the result


@model string
@{
    Layout = null;
}







    
    Home


@Model




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;
}







    
    @Model.Title


@Model.Content






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);
        }


你可能感兴趣的:(ASP.NET,MVC,c#,编程)