문제

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"/>

I know that this problem can be fixed if I could access the full url such as with Url.Action with the

Request.Url.Scheme

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

도움이 되었습니까?

해결책

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;

  }

}

Then use Url.AbsoluteContent("~/media/image.jpeg") in your pages. It will render http://domain/media/image.jpeg

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top