如何将Razor视图转换为字符串?

时间:2021-07-10 04:05:07

I would like to use my Razor view as some kind of template for sending emails, so I would like to "save" my template in a view, read it into controller as a string, do some necessary replacements, and then send it.

我想将我的Razor视图用作发送电子邮件的某种模板,因此我想在视图中“保存”我的模板,将其作为字符串读入控制器,执行一些必要的替换,然后发送它。

I have solution that works: my template is hosted somewhere as an HTML page, but I would like to put it into my application (i.e. in my view). I don't know how to read a view as a string in my controller.

我有解决方案可行:我的模板作为HTML页面托管在某处,但我想把它放到我的应用程序中(即在我的视图中)。我不知道如何在我的控制器中读取视图作为字符串。

2 个解决方案

#1


17  

I use the following. Put it on your base controller if you have one, that way you can access it in all controllers.

我使用以下内容。如果您有基础控制器,请将其放在基础控制器上,这样您就可以在所有控制器中访问它。

public static string RenderPartialToString(Controller controller, string viewName, object model)
{
    controller.ViewData.Model = model;

    using (StringWriter sw = new StringWriter())
    {
        ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
        ViewContext viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
        viewResult.View.Render(viewContext, sw);

        return sw.GetStringBuilder().ToString();
    }
}

#2


6  

Take a look at the RazorEngine library, which does exactly what you want. I've used it before for email templates, and it works great.

看看RazorEngine库,它可以完全满足您的需求。我以前用它来制作电子邮件模板,效果很好。

You can just do something like this:

你可以这样做:

// Read in your template from anywhere (database, file system, etc.)
var bodyTemplate = GetEmailBodyTemplate();

// Substitute variables using Razor
var model = new { Name = "John Doe", OtherVar = "Hello!" };
var emailBody = Razor.Parse(bodytemplate, model);

// Send email
SendEmail(address, emailBody);

#1


17  

I use the following. Put it on your base controller if you have one, that way you can access it in all controllers.

我使用以下内容。如果您有基础控制器,请将其放在基础控制器上,这样您就可以在所有控制器中访问它。

public static string RenderPartialToString(Controller controller, string viewName, object model)
{
    controller.ViewData.Model = model;

    using (StringWriter sw = new StringWriter())
    {
        ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
        ViewContext viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
        viewResult.View.Render(viewContext, sw);

        return sw.GetStringBuilder().ToString();
    }
}

#2


6  

Take a look at the RazorEngine library, which does exactly what you want. I've used it before for email templates, and it works great.

看看RazorEngine库,它可以完全满足您的需求。我以前用它来制作电子邮件模板,效果很好。

You can just do something like this:

你可以这样做:

// Read in your template from anywhere (database, file system, etc.)
var bodyTemplate = GetEmailBodyTemplate();

// Substitute variables using Razor
var model = new { Name = "John Doe", OtherVar = "Hello!" };
var emailBody = Razor.Parse(bodytemplate, model);

// Send email
SendEmail(address, emailBody);