3

How do I get the full URL string when using the relative path nomenclature via tilde? For example, if I want to refer to something in my view using relative path...

~/images/mylogo.jpg

I would like to get the full url such that it would end up looking like ...

https://myserver:8081/images/mylogo.jpg

This is needed to populate the metatags for Open Graph. I am trying to use ...

<meta property="og:image" content="@Url.Content("~/images/mylogo.jpg")" />

... but this only gives relative paths resulting in ...

<meta content="/images/mylogo.jpg" property="og:image">

Details - need to detect if SSL is used or not. Need to determine current port and host name...

Tseng
  • 61,549
  • 15
  • 193
  • 205
barrypicker
  • 9,740
  • 11
  • 65
  • 79
  • you can manually craft the absolute url: `@string.Concat(Context.Request.Scheme, "://", Context.Request.Host, Url.Content("~/images/mylogo.jpg"))` – tmg Nov 15 '16 at 22:55
  • 1
    Possible duplicate of [Getting absolute URLs using ASP.NET Core MVC 6](http://stackoverflow.com/questions/30755827/getting-absolute-urls-using-asp-net-core-mvc-6) – Dawid Rutkowski Nov 16 '16 at 07:12

1 Answers1

3

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...

Community
  • 1
  • 1
barrypicker
  • 9,740
  • 11
  • 65
  • 79