Copied to clipboard

Flag this post as spam?

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


  • BEWD 90 posts 303 karma points
    Feb 16, 2023 @ 09:26
    BEWD
    0

    Enternal Redirect

    Hi Everyone

    I am trying to do an external redirect which I have done many times before in projects but this is the first time in Umbraco 10 and cant get it to work.

    What I normally do is add a text string datatype with the alias externalRedirect and then allow the website author to add a full url (say https://google.com) to redirect the website visitor to whatever the author enters (if anything - most of the time it will be empty so wont redirect)

    In the template I would add the following

    @{
        Layout = "Master.cshtml";
    
        if (Model.HasValue("externalRedirect"))
        {
            Response.Redirect(Model.Value<string>("externalRedirect"));
        }
    }
    

    However this doesn’t work in Umbraco 10 and errors with

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

    Response.Redirect(Model.Value

    I have also done a bit of searching and tried

    {
        Context.Response.Redirect(Url.Content("externalRedirect"));
    }
    

    But that just redirects to say https://localhost:44331/externalRedirect

    Does anyone have any ideas on the quickest and easiest way to get this to work? Im sure its just something really simple that I have missed?

    Thanks

    Ben

  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Feb 16, 2023 @ 11:42
    Huw Reddick
    0

    try adding a return after your redirect

    {
        Context.Response.Redirect("externalRedirect");
        return;
    }
    
  • BEWD 90 posts 303 karma points
    Feb 16, 2023 @ 11:51
    BEWD
    0

    Hi Thanks for the reply Huw

    I have just tried it and unfortunately it makes no difference, just goes to localhost/externalRedirect again.

  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Feb 16, 2023 @ 11:57
    Huw Reddick
    0

    I've not attempted an external redirect before, but I will do some quick tests.

    What is the exact version of Umbraco you are using?

  • BEWD 90 posts 303 karma points
    Feb 16, 2023 @ 11:58
    BEWD
    0

    Thanks Huw, its Umbraco 10.4.0

  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Feb 16, 2023 @ 12:10
    Huw Reddick
    100

    OK, that's a bit strange, I just tried

    Context.Response.Redirect("https://google.com");
    return;
    

    and it redirected fine. If you are still trying Url.Content("externalRedirect") then that is your issue, Url.Content returns a local url. You should be using the code in your first post as the address

    Context.Response.Redirect(Model.Value<string>("externalRedirect"));
    
  • BEWD 90 posts 303 karma points
    Feb 16, 2023 @ 12:30
    BEWD
    0

    Spot on, your right, I have got it working whith the following which is a mixture of what I have previously used and the one I found online.

            if (Model.HasValue("externalRedirect"))
            {
                Context.Response.Redirect(Model.Value<string>("externalRedirect"));
            }
    

    I should have just kept playing around but I just assumed that something had changed in version 10.

    Thanks very much for your help Huw, very kind.

  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Feb 16, 2023 @ 12:33
    Huw Reddick
    0

    No problem. I would still add the return; statement as you can not guarentee that the redirect will occur before other code on the page is executed.

  • BEWD 90 posts 303 karma points
    Feb 16, 2023 @ 12:51
    BEWD
    0

    Great tip thanks, I have just added it in.

Please Sign in or register to post replies

Write your reply to:

Draft