Copied to clipboard

Flag this post as spam?

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


  • nickornotto 397 posts 900 karma points
    Aug 11, 2017 @ 14:34
    nickornotto
    0

    Route hijacking: what will be page url?

    What would be a page url if I use route hijacking?

    Eg. My extended RenderMvcController controller is MyController and my action is MyAction

    Will the page url be /MyController/MyAction/ or the url set up in umbraco for that page which is /my-action/?

    I'm getting page not found when I'm trying either so am not sure if I'm doing it right.

  • Frans de Jong 548 posts 1840 karma points MVP 3x c-trib
    Aug 11, 2017 @ 14:48
    Frans de Jong
    0

    If you Hijack a route than the Url wil not change.

    e.g.

    I have a contentnode that makes use of the documenttype "Specialpage". with the url www.site.com/somepage.

    If I want to hijack the route I can do so by making a SpecialpageController.cs. (Documenttypealias + Controller). In that file I can do whatever I need to do.

    The Url stays the same but now the code in the controller gets executed as well.

    More info can be found here

  • nickornotto 397 posts 900 karma points
    Aug 11, 2017 @ 15:40
    nickornotto
    0

    So that's strange because my page url shows page not found and when I try to debug the controller is not hit

  • Frans de Jong 548 posts 1840 karma points MVP 3x c-trib
    Aug 11, 2017 @ 18:49
    Frans de Jong
    0

    Can you show some code?

  • nickornotto 397 posts 900 karma points
    Aug 14, 2017 @ 14:42
    nickornotto
    0

    Model:

    namespace MyDomain.Models
    {
        public class MyViewModel : RenderModel
        {
            public MyViewModel(IPublishedContent content) : base(content) { }
    
            public string MyContent { get; set; }
        }
    }
    

    Controller:

    namespace MyDomain.Controllers
    {
        public class MyControllerController : RenderMvcController
        {
            public override ActionResult Index(RenderModel model)
            {
                return base.View(model);
            }
    
            public ActionResult MyAction(RenderModel model)
            {
                int type = model.Content.DocumentTypeId;
                var myCustomModel = new MyViewModel(model.Content) { MyContent = "aa" };
    
                return CurrentTemplate(myCustomModel);
            }
        }
    }
    

    View:

    @using MyDomain.Models;
    @inherits Umbraco.Web.Mvc.UmbracoViewPage<MyViewModel>
    @{
        Layout = "Master.cshtml";
    }
    
    <div class="row">
        @Model.Content.Name
    </div>
    

    With the above I'm getting:

    Page Not Found
    No umbraco document matches the url '/page-edit/'.
    

    which is the umbraco generated url to my page and which was working before I added route hijacking.

  • Frans de Jong 548 posts 1840 karma points MVP 3x c-trib
    Aug 14, 2017 @ 15:47
    Frans de Jong
    0

    I see 2 things that I do differently:

    For the model I use the model generated by modelsbuilder and extend it so it will be:

    namespace Umbraco.Web.PublishedContentModels
    {
        public partial class [Modelsbuilder modelname you want to extend]
       {
           public string MyContent { get; set; }
       }
    }
    

    In my view I use:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.SiteContentPage>
    @{
        Layout = "Master.cshtml";
    }
    
    <div class="row">
        @Model.Content.MyContent
    </div>
    

    I'm not sure what you are trying to do in your controller.

    If you want to render the page I'd use:

    public class [Modelsbuilder modelname]Controller : RenderMvcController
    {
       public ActionResult Index([Modelsbuilder modelname] model)
      {
          //assign value to the custom propert
          return CurrentTemplate(model);
       }
    }
    

    if you want to post something or make a partial action you can use:

    public class [Modelsbuilder modelname]SurfaceController : SurfaceController
    {
        [HttpPost]
        public ActionResult index ([custom model] model)
        {
            //Do stuff
            return RedirectToCurrentUmbracoPage();
        }
    
    }
    
  • nickornotto 397 posts 900 karma points
    Aug 15, 2017 @ 09:25
    nickornotto
    0

    Sorry but I don't get what you suggesting:

    1. what is [Modelsbuilder modelname you want to extend]?

    2. Why I place it under Umbraco.Web.PublishedContentModels namespace? And where do I extend it? In my custom model?

    3. What is [ContentModels.SiteContentPage]? - the model type in the view?

    4. What is [Modelsbuilder modelname] in your controller? - is it umbraco template alias or document type alias?

    5. How in the view Model can have Content property if it doesn't take IPublishedContent in constructor?

    I simply based my code on this article: https://our.umbraco.org/documentation/reference/routing/custom-controllers

    What you suggesting is very different and not fully described so I am not able to replicate the full working code for now.

  • nickornotto 397 posts 900 karma points
    Aug 15, 2017 @ 09:32
    nickornotto
    0

    To clarify my code I could change it to:

    namespace MyDomain.Controllers
    {
        public class MyUmbracoDocTypeController : RenderMvcController
        {
            public ActionResult MyUmbracoTemplate(RenderModel model)
            {
                int type = model.Content.DocumentTypeId;
                var myCustomModel = new MyViewModel(model.Content) { MyContent = "aa" };
    
                return CurrentTemplate(myCustomModel);
            }
        }
    }
    

    (Ignore Index action)

  • Al Burns 49 posts 149 karma points
    Aug 16, 2017 @ 09:35
    Al Burns
    0

    Hi manila.

    Make sure your controller name matches the alias name you have given your document type, so if your document type has an alias of "articlePage" then your controller must be called "ArticlePageController".

    For your controller action, the way I do it is make sure it has the same name as the template, so if your template is "ArticlePage.cshtml" then the controller action should be called "ArticlePage".

    For your view model, typically what I do is not derive from the RenderModel base class, return View(model) from my controller action and change the inherits statement of my template to:

    @inherits UmbracoViewPage<MyViewModel>
    

    Hope this helps.

  • nickornotto 397 posts 900 karma points
    Aug 16, 2017 @ 17:01
    nickornotto
    0

    That's what I did. Not sure why it wasn't working but I redid the whole thing and now it works without a problem.

    Thanks all

Please Sign in or register to post replies

Write your reply to:

Draft