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
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
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
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
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
try adding a return after your redirect
Hi Thanks for the reply Huw
I have just tried it and unfortunately it makes no difference, just goes to localhost/externalRedirect again.
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?
Thanks Huw, its Umbraco 10.4.0
OK, that's a bit strange, I just tried
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 addressSpot 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.
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.
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.Great tip thanks, I have just added it in.
is working on a reply...