1

在下面的 AS:NET / Mono MVC2 方法中, 将视图呈现为字符串用于创建 html 电子邮件正文。

Partiil 视图 Order.ascx 包含像

    <img width='20%' alt='' src='<%= Url.Action("Logo", "Store")%>' />

在电子邮件中,这些图像出现时没有站点地址/Store/Logo,因此不会出现图像。如何强制图像链接以绝对地址显示,htttp:/Mysite.com/Store/Logo或将站点基地址添加到 html 电子邮件正文,这是有帮助的。

使用 ASP.NET/Mono MVC2、.NET 3.5、jquery、jquery ui。

控制器动作方法:

        var s = RenderViewToString<OrderConfirm>("~/Views/Checkout/Order.ascx", order);

public class ControllerBase : Controller
{
    protected string RenderViewToString<T>(string viewPath, T model)
    { 
        ViewData.Model = model;
        using (var writer = new StringWriter())
        {
            var view = new WebFormView(viewPath);
            var vdd = new ViewDataDictionary<T>(model);
            var viewCxt = new ViewContext(ControllerContext, view, vdd, new TempDataDictionary(), writer);
            viewCxt.View.Render(viewCxt, writer);
            return writer.ToString();
        }
    }
4

1 回答 1

3

只需使用proper overload

<%= Url.Action("Logo", "Store", null, "http") %>

由于我们已经指定了协议,帮助程序将生成一个绝对 url。此外,如果您不想对其进行硬编码,则可以从当前请求中读取它:

<%= Url.Action("Logo", "Store", null, Request.Url.Scheme) %>

Now this will work for both standard and HTTPS schemes depending on how the view is requested.

于 2013-03-11T12:42:31.233 回答