1

In AS:NET / Mono MVC2 method below form Render a view as a string is used to create html e-mail bodies.

Partiil view Order.ascx contains images like

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

In Emails those images appear without site address like /Store/Logo and thus images do not appear. How to force image links to appear with absolute addresses like htttp:/Mysite.com/Store/Logo or add site base address to html email body is this helps.

ASP.NET / Mono MVC2 , .NET 3.5, jquery, jquery ui are used.

Controller Action method:

        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();
        }
    }
Community
  • 1
  • 1
Andrus
  • 26,339
  • 60
  • 204
  • 378

1 Answers1

3

Just use the proper overload:

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

Since we've specified the protocol, the helper will generate an absolute url. Also if you don't want to hardcode it you could read it from the current request:

<%= 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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Same view is also used in site to show order in browser. In this case absolute addresses appear is browser also. Is this OK ? – Andrus Mar 11 '13 at 12:43
  • Yeah, it's fine. No problems with that. – Darin Dimitrov Mar 11 '13 at 12:44
  • You just don't want to hard-code absolute URLs, there's nothing wrong with using them other than that, so as long as they're generated, every link could be absolute for all it matters. – Chris Pratt Mar 11 '13 at 12:49