获得绝对路径的图像在电子邮件发送 [英] get absolute path for an image to send in email

查看:120
本文介绍了获得绝对路径的图像在电子邮件发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个观点(一个或多个图像),我也想作为电子邮件的身体使用。服务于视图使用模型 Url.Content()来获取绝对图像路径。这种方法工作正常的网页,但是当我呈现完全相同的视图作为电子邮件的正文,则图像无法找到这是由从呈现的HTML不完整的路径建议。

I have a view (with one or more images) that I also want to use as the body of an email message. The Model serving the view uses Url.Content() to retrieve the absolute image path. This approach works fine for web pages, but when I render the exact same view as the body of the email, then the image can not be found which is suggested by the incomplete path from the rendered html.

 <img src="/Media/fe882a1d-3b77-4264-ab91-cade985ecbed.JPG"/>

我知道,这个问题是可以固定的,如果我可以访问完整的URL,例如用 Url.Action

Request.Url.Scheme

过载协议。有没有一种方法来确定从 Url.Content 完全限定的网址是什么?

overload for protocol. Is there a way to determine the fully qualified URL from Url.Content?

推荐答案

尝试书面方式自己的URL扩展方法和 Uri.GetLeftPart和放大器的帮助下解决; UriPartial.Authority

Try writting your own url extension method and resolve with the help of Uri.GetLeftPart & UriPartial.Authority

public static class UrlExtensions
{
  public static string AbsoluteContent(this UrlHelper urlHelper
                                     , string contentPath)
  {
   Uri requestUrl = urlHelper.RequestContext.HttpContext.Request.Url;

   string absolutePath = string.Format("{0}{1}",
                          requestUrl.GetLeftPart(UriPartial.Authority),
                          urlHelper.Content(contentPath));
   return absolutePath;

  }

}

然后使用 Url.AbsoluteContent(〜/媒体/ image.jpeg)在页面中。这将使 HTTP://domain/media/image.jpeg

这篇关于获得绝对路径的图像在电子邮件发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆