如何从视图生成字符串(.aspx 或 .ascx)

发布于 2024-10-26 07:35:33 字数 152 浏览 1 评论 0原文

如何以字符串格式获取已解析(<%%> 已解析)视图(aspx 或 ascx)?我想要包含一些 <%= ... %> 代码块的 .ascx 文件,并且我希望能够将其作为 HTML 格式的电子邮件的一部分发送。我怎样才能用 MVC 做到这一点?

How can I get the resolved (<%%> resolved) view (aspx or ascx) in a string format? I want to have .ascx file with some <%= ... %> code blocks and I want to be able to send it as part of e-mail in HTML format. How can I do this with MVC?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

说不完的你爱 2024-11-02 07:35:33

不确定这是否适用于部分视图,但我使用这种和平的代码将视图呈现为字符串。

public string RenderViewAsString(ControllerContext context, string viewName, object model)
{
    if (string.IsNullOrEmpty(viewName))
        viewName = context.RouteData.GetRequiredString("action");

    var view = ViewEngines.Engines.FindView(context, viewName, null).View;
    if (view != null)
    {
        var sb = new StringBuilder();
        using (var writer = new StringWriter(sb))
        {
            var viewContext = new ViewContext(context, view,
                    new ViewDataDictionary(model), new TempDataDictionary(), writer);
            view.Render(viewContext, writer);
            writer.Flush();
        }
        return sb.ToString();
    }
    return string.Empty;
}

Not sure if this works with partial views, but I use this peace of code to render a view as a string.

public string RenderViewAsString(ControllerContext context, string viewName, object model)
{
    if (string.IsNullOrEmpty(viewName))
        viewName = context.RouteData.GetRequiredString("action");

    var view = ViewEngines.Engines.FindView(context, viewName, null).View;
    if (view != null)
    {
        var sb = new StringBuilder();
        using (var writer = new StringWriter(sb))
        {
            var viewContext = new ViewContext(context, view,
                    new ViewDataDictionary(model), new TempDataDictionary(), writer);
            view.Render(viewContext, writer);
            writer.Flush();
        }
        return sb.ToString();
    }
    return string.Empty;
}
撩人痒 2024-11-02 07:35:33

不要采取困难的方式,请查看 MvcMailer邮政。这将使您的生活变得更加轻松。不仅如此,您还将有更多的时间专注于解决一些实际的业务问题,而不是像这样已经解决的问题。

Don't do it the hard way, checkout MvcMailer or Postal. It will make your life much easier. And not only that but you will have more time to focusing on solving some real business problems rather than plumbing stuff like this which have already been addressed.

寒尘 2024-11-02 07:35:33

您可以使用 MailMessage 类来生成邮件。您可以在单独的自定义操作过滤器中推出电子邮件创建逻辑,以保持控制器操作的精简和电子邮件逻辑的可重用性。

[更新]
自定义操作过滤器可以是这样的:

public class SendEmailAttribute : ActionFilterAttribute
 {

    public SendEmailAttribute()
    {
        //Initialize logic --Dependency injection
    }
    public override void OnActionExecuted(ActionExecutedContext context)
    {
        SendMail(context); //Implement email sending logic using MailMessage
    }
 }

You can make use of the MailMessage class to generate the mail. You can roll out the email creation logic in a separate custom action filter to keep the controller action lean and the email logic reusable.

[UPDATE]
The custom action filter can be like :

public class SendEmailAttribute : ActionFilterAttribute
 {

    public SendEmailAttribute()
    {
        //Initialize logic --Dependency injection
    }
    public override void OnActionExecuted(ActionExecutedContext context)
    {
        SendMail(context); //Implement email sending logic using MailMessage
    }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文