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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Same view is also used in site to show order in browser. In this case absolute addresses appear is browser also. Is this OK ?
Yeah, it's fine. No problems with that.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.