Copied to clipboard

Flag this post as spam?

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


  • Nathan Reece 28 posts 199 karma points
    3 days ago
    Nathan Reece
    0

    Umbraco 8 Where to store custom Code relating to Views

    Hey guys one of my content pages has a lot of custom code currently I have all my code in the razor script and my code is really messy,

    Where do you guys store all your custom code?

    Typically i store everything in a controller but with Umbraco I'm a little confused how this happens?

    Please Help!!

  • David Armitage 314 posts 1239 karma points
    3 days ago
    David Armitage
    100

    Hi Nathan,

    Good question. I struggled with this when I first started out. My code was quite messy on complicated projects because I stored a load of .net code in Razor views which usually would have been stored in controllers.

    That's until I found out about Route Hijacking. https://our.umbraco.com/documentation/reference/routing/custom-controllers

    So Umbraco still follows the usual MVC pattern but the controller is hidden or a better description used by Umbraco (RenderMvcController).

    Fortunately it is possible to override this and do what's known as Route Hijacking. Have a look at the link I shared about. It's actually very straight forward.

    1. You create a class with inherits from RenderMvcController

    2. You have an override method called Index

    3. Then you put all your custom code in here.

    4. You can also create your own view model to store the usual Umbraco content as well as any other properties you want to store and push to the view.

    This is a game changer and has really improved the speed and tidiness I can build Umbraco websites.

    I would 100% take a look into that.

    Regards

    David

  • Nathan Reece 28 posts 199 karma points
    3 days ago
    Nathan Reece
    0

    Thankyou David,

    I will take a look into that now, Sounds like that is what i was looking for :)

  • Nathan Reece 28 posts 199 karma points
    3 days ago
    Nathan Reece
    0

    If any one else is having dificulties with this i have written a Blog to try help out here is a link : https://www.umbrajobs.com/blog/posts/2020/july/route-hijacking-in-umbraco-8/

Please Sign in or register to post replies

Write your reply to:

Draft