Copied to clipboard

Flag this post as spam?

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


  • Matt Taylor 873 posts 2086 karma points
    Jan 23, 2014 @ 11:52
    Matt Taylor
    0

    Response.Redirect

    I want a template that will just redirect the user to the parent page for certain nodes that shouldn't be accessed.

    I've created a template/view which contains the following

    Response.Redirect(@Model.Content.Parent.Url, true);

    however it does not appear to be working, I get just a blank page and the URL shows the page that we are supposed to be redirecting from.

    I've used this before with web forms so is there something in the framework that is stopping this from working? Some kind of special routing?

    Regards,

    Matt

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 23, 2014 @ 12:04
    Jeroen Breuer
    100

    Hello,

    There isn't any special kind of routing. What you're trying to do could also be done in a Controller, but I don't know if that will make any difference. You could also just use the umbracoRedirect property and select the parent node through the picker.

    Jeroen

  • Matt Taylor 873 posts 2086 karma points
    Jan 23, 2014 @ 12:52
    Matt Taylor
    0

    Thanks Jeroen,

    I just wanted to make sure there wasn't a reason before I invest some time making it work.

    I don't want to use umbracoRedirect because I'm wanting to use it with the DateFolders package. There will be lots of these folders and I want something automatic.

    Regards,

    Matt

  • Matt Taylor 873 posts 2086 karma points
    Jan 23, 2014 @ 13:55
    Matt Taylor
    0

    I can ertainly make it work using RedirectToUmbracoPage() like you have shown with the Contact controller.

    return RedirectToUmbracoPage(child);

    I've got my RedirectToParent View already (it had the non working code in it).
    To make it work through the a controller it means I'll have to also create a RedirectToParentModel and RedirectToParentController.

    I'm happy to do this but is seems like a lot of extra things just to make the redirect work. The new model in particular seems pointless as it won't be adding anything.
    Is there a better way?

    Thanks, Matt

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 23, 2014 @ 14:01
    Jeroen Breuer
    0

    Can you show how your current code looks like. If you have a normal template that doesn't have custom model you can also do this in the controller:

    public class ContactController : BaseSurfaceController
    {
        public ActionResult Contact(RenderModel model)
        {
            //Do logic here without custom model   
    
            return CurrentTemplate(model);
        }
    }

    That will also work. The model will be created by Umbraco. It's the same as in these examples: http://our.umbraco.org/documentation/Reference/Mvc/custom-controllers

    Jeroen

  • Matt Taylor 873 posts 2086 karma points
    Jan 23, 2014 @ 14:41
    Matt Taylor
    1

    Thanks Joroen,

    The answer occurred to me whilst having lunch and my mind was off the subject.

    I have done as you have suggested I think, created a new action in the default controller and then just called it in the template view.

    public ActionResult RedirectToParent()
    {
          return Redirect(CurrentPage.Parent.Url);
    }

    I'm slowly getting to grips with the MVC way of doing things. I'm a big advocate of your Hybrid framework for learning it.

    Cheers, Matt

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 23, 2014 @ 14:44
    Jeroen Breuer
    0

    Adding that ActionResult to the default controller is a great solution! Nice thinking :-).

    Jeroen

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 23, 2014 @ 14:49
    Jeroen Breuer
    0

    Maybe I misread. Do you call that method in your template view with an Html.Action? 

    If the name of the template is the same as the name of the ActionResult in the default controller you might be able to route hijack to it. 

    Also you can do a redir like this:

    return RedirectToUmbracoPage(CurrentPage.Parent)

    Not really sure what the benefit is ;-).

    Jeroen

  • Matt Taylor 873 posts 2086 karma points
    Jan 23, 2014 @ 15:14
    Matt Taylor
    0

    Ah, I get it. Yes the ActionResult is the same name as the template so I've taken out the explicit call to Html.Action and it still works. :-)

    I've also made use of

    RedirectToUmbracoPage

    Cheers, Matt

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 23, 2014 @ 15:16
    Jeroen Breuer
    0

    Your template isn't even hit right know because it will first hit the controller before the template and that's where you do the redirect. Might be good do add some comment in the view that it's not getting hit and only is used for redir in the controller. Did that myself too a couple of times :-).

    Jeroen

  • Matt Taylor 873 posts 2086 karma points
    Jan 23, 2014 @ 15:55
    Matt Taylor
    0

    Ah that's good to know. I was wondering how the flow worked.

Please Sign in or register to post replies

Write your reply to:

Draft