The possible duplicate answer at Getting absolute URLs using ASP.NET Core MVC 6 was very similar, but did not address the use of the tilde. For the tilde I used the URL Helper 'Content' method. For completeness here is where I landed...
Create extension method...
using Microsoft.AspNetCore.Mvc;
namespace testProject.Utilities
{
public static class MVCExtensionMethods
{
public static string BaseUrl(this IUrlHelper helper)
{
var url = string.Format("{0}://{1}", helper.ActionContext.HttpContext.Request.Scheme, helper.ActionContext.HttpContext.Request.Host.ToUriComponent());
return url;
}
public static string FullURL(this IUrlHelper helper, string virtualPath)
{
var url = string.Format("{0}://{1}{2}", helper.ActionContext.HttpContext.Request.Scheme, helper.ActionContext.HttpContext.Request.Host.ToUriComponent(), helper.Content(virtualPath));
return url;
}
}
}
Use in View via Razor...
@using testProject.Utilities
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta property="og:url" content="@Url.BaseUrl()" />
<meta property="og:type" content="website" />
<meta property="og:title" content="@ApplicationConstants.ApplicationTitle" />
<meta property="og:description" content="@ApplicationConstants.TagLine" />
<meta property="og:image" content="@Url.FullURL("~/images/logo-black.png")" />
<title>@ApplicationConstants.ApplicationTitle</title>
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
@Html.ApplicationInsightsJavaScript(TelemetryConfiguration)
</head>
<body>
</body>
</html>
Summary...
MVC has two kinds of helpers - HTML Helpers and URL helpers. I was trying to get the URL when using the HTML helpers. Should have been using the URL helper instead. The possible dup answer directed me to look into the URL helper instead. It did not show the use of the tilde virtual directory nomenclature though...