Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Meni 271 posts 528 karma points
    Feb 20, 2023 @ 16:40
    Meni
    0

    @Request.Url.Scheme, @Request.Url.Host in Umbraco 11

    Hi,

    how do I get @Request.Url.Scheme, @Request.Url.Host in Umbraco 11?

    I saw some some discussions on stackoverflow that it was depreciated in .NET core

    For example, in one answer they suggest to use

    var url = httpContextAccessor.HttpContext?.Request?.GetDisplayUrl();
    

    but I'm not sure how to get in Umbrco @Request.Url.Scheme, and @Request.Url.Host

    I need for the social media tags - this is my Umbraco 7 code:

    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage
    
    @{
    var home = Model.AncestorOrSelf(1);
    
    var ogType = "article";
    
    string ogImage = string.Empty;
    if (Model.HasValue("articleImg"))
    {
        ogImage = String.Format("{0}://{1}{2}", @Request.Url.Scheme, @Request.Url.Host, @Umbraco.Media(CurrentPage.articleImg.ToString()).Url);
    }
    
    string TwitterHandle = "";
    TwitterHandle = "@" + "opli";
    
    
    <!--Facebook Open Graph-->
    <meta property="og:locale" content="en_US" />
    <meta property="og:type" content="@ogType" />
    <meta property="og:title" content="@Umbraco.Field("title")" />
    <meta property="og:description" content="@Umbraco.Field("metaDescription")" />
    <meta property="og:url" content="@Request.Url" />
    

    Thanks

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Feb 20, 2023 @ 20:06
    Huw Reddick
    100

    inject IHttpContextAccessor and do something like

     var urlRequest = _httpContextAccessor.HttpContext.Request;
    

    You can then use

    urlRequest.Scheme
    urlRequest.Host
    
  • Meni 271 posts 528 karma points
    Feb 21, 2023 @ 05:55
    Meni
    0

    Thank you so much :)

    It works now!

Please Sign in or register to post replies

Write your reply to:

Draft