1

I want to get the absolute path of a static file using @Url.Content on a razor view to use it as a value of content attr of a meta tag but It's seems not working for me I didn't know what is the problem.

@{
var pageThumbnail = Url.Content("~/assets/121545/mid.jpg");
}

@section Meta {
<meta property="og:image" content="@pageThumbnail" />
}
  • Result : <meta property="og:image" content="/assets/121545/mid.jpg" />

  • I expected this result instead: <meta property="og:image" content="https://localhost:5001/assets/121545/mid.jpg" />

Yiyi You
  • 16,875
  • 1
  • 10
  • 22
byteram
  • 125
  • 8
  • What you are asking for is not supported yet, see https://github.com/dotnet/aspnetcore/issues/14192. You can follow the information in https://stackoverflow.com/questions/30755827/getting-absolute-urls-using-asp-net-core if you want to use absolute URLs. – Professor of programming Jun 09 '22 at 22:30
  • Does this answer your question? [Getting absolute URLs using ASP.NET Core](https://stackoverflow.com/questions/30755827/getting-absolute-urls-using-asp-net-core) – Professor of programming Jun 09 '22 at 22:31
  • There's an overload `Url.Content("...", Request.Scheme, Request.Host)` – Jeremy Lakeman Jun 10 '22 at 02:22

1 Answers1

1

Try to add Context.Request.Host to pageThumbnail:

@{
var pageThumbnail = Context.Request.Host + Url.Content("~/assets/121545/mid.jpg");
}

@section Meta {
<meta property="og:image" content="@pageThumbnail" />
}
Yiyi You
  • 16,875
  • 1
  • 10
  • 22