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.
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.
You create a class with inherits from RenderMvcController
You have an override method called Index
Then you put all your custom code in here.
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.
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!!
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.
You create a class with inherits from RenderMvcController
You have an override method called Index
Then you put all your custom code in here.
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
Thankyou David,
I will take a look into that now, Sounds like that is what i was looking for :)
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/
is working on a reply...