Copied to clipboard

Flag this post as spam?

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


  • Peter van Geffen 54 posts 290 karma points
    Oct 25, 2022 @ 08:39
    Peter van Geffen
    0

    The name 'Request' does not exist in the current context

    We have in our old Umbraco (v8) a 'GetSocialMeta' partial. Obviously it won't work in our new Umbraco (v10) projects.

    This is the code:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @{
        string title = String.Empty;
        string hyperlink = String.Format("https://{0}{1}", Request.Url.Host, Request.Url.AbsolutePath);
        string ogImageUrl = String.Empty;
        string twitterName = String.Empty;
        string canonicalURL = String.Empty;
        string omschrijving = String.Empty;
        string twitterTitle = String.Empty;
    }
    @if (Model.HasValue("canonicalURL")){
        canonicalURL = Model.Value<string>("canonicalURL");
    }
    else{
        canonicalURL = @hyperlink;
    }
    @if (Model.HasValue("browserTitle")){
        title = Model.Value<string>("browserTitle");
    }else if (Model.HasValue("paginaTitel")){
        title = Model.Value<string>("paginaTitel");
    }else{
        title = Model.Name;
    }
    @if (Model.Value<string>("twitterNaam", fallback: Fallback.ToAncestors) != String.Empty){
        twitterName = Model.Value<string>("twitterNaam", fallback: Fallback.ToAncestors);
    }
    @if (Model.Value<IPublishedContent>("ogImage", fallback: Fallback.ToAncestors) != null){
        IPublishedContent ogImage = Model.Value<IPublishedContent>("ogImage", fallback: Fallback.ToAncestors);
        ogImageUrl = String.Format("http://{0}{1}", Request.Url.Host, ogImage.Url);
    }else{
        ogImageUrl = String.Format("http://{0}/images/logo-fb.png", Request.Url.Host);
    }
    @if (Model.HasValue("twitterTitel")){
        twitterTitle = Model.Value<string>("twitterTitel");
    }else{
        twitterTitle = title; 
    }
    @if (Model.HasValue("omschrijving")){
        omschrijving = Model.Value<string>("twitterOmschrijving");
    }else{
        omschrijving = Model.Value<string>("metaDescription");
    }
    @{ 
        <meta property="og:title" content="@title" />
        <meta property="og:type" content="website" />
        <meta property="og:url" content="@hyperlink" />
        <meta property="og:image" content="@ogImageUrl" />
        <meta property="og:description" content="@omschrijving" />
        <meta name="twitter:site" content="@twitterName" />
        <meta name="twitter:creator" content="@twitterName" />
        <meta name="twitter:card" content="summary_large_image" />
        <meta name="twitter:title" content="@twitterTitle" />
        <meta name="twitter:description" content="@omschrijving">
        <meta name="twitter:image" content="@ogImageUrl" />
        <link rel="canonical" href="@canonicalURL">
    }
    

    These are the errors we get: An error occurred during the compilation of a resource required to process this request. Please review the following specific error details and modify your source code appropriately.

    The name 'Request' does not exist in the current context
    +
        string hyperlink = String.Format("https://{0}{1}", Request.Url.Host, Request.Url.AbsolutePath);
    The name 'Request' does not exist in the current context
    +
        string hyperlink = String.Format("https://{0}{1}", Request.Url.Host, Request.Url.AbsolutePath);
    The name 'Request' does not exist in the current context
    +
        ogImageUrl = String.Format("http://{0}{1}", Request.Url.Host, ogImage.Url);
    The name 'Request' does not exist in the current context
    +
        ogImageUrl = String.Format("http://{0}/images/logo-fb.png", Request.Url.Host);
    

    Is there a way to fix this or a better way to set it up?

  • Huw Reddick 1932 posts 6722 karma points MVP 2x c-trib
    Oct 25, 2022 @ 10:37
    Huw Reddick
    0

    try Context.Request

  • Huw Reddick 1932 posts 6722 karma points MVP 2x c-trib
    Oct 25, 2022 @ 10:39
    Huw Reddick
    0

    although in .net core there is no Request.url.Host , it is just Request.Host

  • Peter van Geffen 54 posts 290 karma points
    Oct 25, 2022 @ 10:45
    Peter van Geffen
    0

    It unfortunately still returns the same errors

  • Huw Reddick 1932 posts 6722 karma points MVP 2x c-trib
    Oct 25, 2022 @ 14:52
    Huw Reddick
    0

    just noticed

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    

    That is not correct for Umbraco 9, IIRC it should be

    Umbraco.Cms.Web.Common.Views.UmbracoViewPage
    
  • Peter van Geffen 54 posts 290 karma points
    Oct 26, 2022 @ 05:22
    Peter van Geffen
    0

    You're right about that. But it isn't the solution

  • Huw Reddick 1932 posts 6722 karma points MVP 2x c-trib
    Oct 26, 2022 @ 07:46
    Huw Reddick
    0

    not sure why, works ok for me

    Context.Request

  • Peter van Geffen 54 posts 290 karma points
    Oct 26, 2022 @ 07:53
    Peter van Geffen
    0

    Probably because i'm a pretty noob in back-end coding. I've no idea how to get this work in our code (above)

  • Huw Reddick 1932 posts 6722 karma points MVP 2x c-trib
    Oct 26, 2022 @ 10:06
    Huw Reddick
    100

    I'm not sure either as it just works for me. I just did a quick test and created a partialView as below

    @inherits UmbracoViewPage
    @{
    
        string hyperlink = String.Format("https://{0}{1}", Context.Request.Host, Context.Request.Path);
    
    }
    

    I then included it in my main page @await Html.PartialAsync("Layout/_TestPartial") and it works with no errors

  • Peter van Geffen 54 posts 290 karma points
    Oct 26, 2022 @ 12:29
    Peter van Geffen
    0

    This works indeed. But how do i get it to work for:

    ogImageUrl = String.Format("http://{0}{1}", Request.Url.Host, ogImage.Url);
    

    &

    ogImageUrl = String.Format("http://{0}/images/logo-fb.png", Request.Url.Host);
    

    Because if i adjust it the same way it still won't work

  • Huw Reddick 1932 posts 6722 karma points MVP 2x c-trib
    Oct 26, 2022 @ 12:36
    Huw Reddick
    0

    just replace Request.Url.Host with Context.Request.Host

  • Peter van Geffen 54 posts 290 karma points
    Oct 26, 2022 @ 12:45
    Peter van Geffen
    0

    I was too quick with my reply (deleted) Just replace them gives me other errors

    Argument 1: cannot convert from 'string' to 'System.IFormatProvider?'
    +
        ogImageUrl = String.Format("http://{0}{1}", Context.Request.Host, ogImage.Url);
    Argument 2: cannot convert from 'Microsoft.AspNetCore.Http.HostString' to 'string'
    +
        ogImageUrl = String.Format("http://{0}{1}", Context.Request.Host, ogImage.Url);
    Argument 3: cannot convert from 'method group' to 'object?'
    +
        ogImageUrl = String.Format("http://{0}{1}", Context.Request.Host, ogImage.Url);
    
  • Huw Reddick 1932 posts 6722 karma points MVP 2x c-trib
    Oct 26, 2022 @ 12:51
    Huw Reddick
    100

    I think it may need to be ogImage.Url() not ogImage.Url

  • Peter van Geffen 54 posts 290 karma points
    Oct 26, 2022 @ 13:01
    Peter van Geffen
    0

    Ah f*ck, that's it. I still have to get used to that extra () compared to Umbraco 8.

    Thanks for your help and above all patience.

  • Peter van Geffen 54 posts 290 karma points
    Oct 26, 2022 @ 13:02
    Peter van Geffen
    0

    Up to the next problem :P

  • Huw Reddick 1932 posts 6722 karma points MVP 2x c-trib
    Oct 26, 2022 @ 13:33
    Huw Reddick
    1

    Happy to help :) had to go through the same pain myself :D

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies