Copied to clipboard

Flag this post as spam?

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


  • Matt 358 posts 841 karma points
    Jan 27, 2023 @ 15:55
    Matt
    0

    Redirect Template page

    Hi all,

    I had a redirect page on umbraco 8 with the following template

    @using Umbraco.Cms.Web.Common.PublishedModels;
    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<ContentModels.RedirectPage>
    @using ContentModels = Umbraco.Cms.Web.Common.PublishedModels;
    @{
        Layout = "Master.cshtml";
    
            var links = Model.Value<IEnumerable<Link>>("linkTarget");
        if (links != null && links.Any())
        {
            var prettyLink = links.FirstOrDefault();
            Response.RedirectPermanent(prettyLink.Url);
        }
    }
    

    The document type had a URL picker which if you loaded up that page it would redirect you to x page. For some reason this isnt working in Umbraco 10, has something changed?

    Thanks

    Matt

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Jan 27, 2023 @ 16:55
    Huw Reddick
    0

    Hi Matt,

    try adding a return statement after the redirect.

    if (links != null && links.Any())
    {
        var prettyLink = links.FirstOrDefault();
        Response.RedirectPermanent(prettyLink.Url);
        return;
    }
    
  • Matt 358 posts 841 karma points
    Feb 01, 2023 @ 13:47
    Matt
    0

    Thanks for the reply, Unfortnetly still no joy. doesn't redirect :(

  • Matt 358 posts 841 karma points
    Feb 01, 2023 @ 14:01
    Matt
    0

    I get the following

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

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Feb 01, 2023 @ 14:49
    Huw Reddick
    0

    It should be Context.Response

  • Matt 358 posts 841 karma points
    Feb 01, 2023 @ 15:06
    Matt
    0

    Hello Huw

    Thanks for getting back to me, unfortunately I'm getting the following now.

    HttpResponse' does not contain a definition for 'RedirectPermanent' and no accessible extension method 'RedirectPermanent' accepting a first argument of type 'HttpResponse' could be found (are you missing a using directive or an assembly reference?)

    Thanks

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Feb 01, 2023 @ 15:35
    Huw Reddick
    1

    try this

    Context.Response(prettyLink.Url,true);
    
  • Matt 358 posts 841 karma points
    Feb 01, 2023 @ 16:06
    Matt
    0

    Sorry Huw still no joy, I thought it would be simple :(

    Non-invocable member 'HttpContext.Response' cannot be used like a method.

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Feb 01, 2023 @ 16:09
    Huw Reddick
    0

    where exactly are you trying to do this?

    Could you show some more of your code maybe.

  • Matt 358 posts 841 karma points
    Feb 01, 2023 @ 16:12
    Matt
    0

    Hi Huw,

    So I have a doc type in my compositions page called "Redirect Page" inside that I have a url picker.

    Attached to the doc type is a template currently with the following;

    @using Umbraco.Cms.Web.Common.PublishedModels;
    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<ContentModels.RedirectPage>
    @using ContentModels = Umbraco.Cms.Web.Common.PublishedModels;
    @using Umbraco.Cms.Core.Models;
    @using System;
    
    
    @{
        Layout = "Master.cshtml";
    
            var links = Model.Value<IEnumerable<Link>>("linkTarget");
    if (links != null && links.Any())
    {
        var prettyLink = links.FirstOrDefault();
        Context.Response(prettyLink.Url,true);
        return;
    }
    }
    
  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Feb 01, 2023 @ 16:21
    Huw Reddick
    102

    sorry, my typo :)

    Context.Response.Redirect(prettyLink.Url,true);
    
  • Matt 358 posts 841 karma points
    Mar 17, 2023 @ 09:01
    Matt
    0

    Hi,

    This code works fine but how can I get it to open in a new window?

    @{
        Layout = "Master.cshtml";
    
            var links = Model.Value<IEnumerable<Link>>("linkTarget");
    if (links != null && links.Any())
    {
        var prettyLink = links.FirstOrDefault();
        Context.Response.Redirect(prettyLink.Url,true);
    
    }
    }
    

    Thanks

  • DavidDBD 3 posts 23 karma points
    Jun 05, 2023 @ 09:34
    DavidDBD
    0

    This code works fine but how can I get it to open in a new window?

    Its been a while so you probably figured this out, but - you cant.

    By using Response.Redirect what you are doing is telling the server to send a 302 status back to the client with the location header set to a URL, that in turn tells the browser that it should do another GET request to the new URL.

    If you want to open a new window, that would have to be handled client side and you wouldn't be able to do it with a redirect which originated on the server (Unless you do some weird stuff to intercept the response, stop the browser redirecting and then open the new windows... I would not recommend attempting that).

    If I had this requirement I'd probably be looking to do it with client side JavaScript rather than having the roundtrip back to the server if you KNOW that its just going to result in another GET request anyway.

Please Sign in or register to post replies

Write your reply to:

Draft