Reputation: 1742
I am using Razor Web Pages (so not MVC). I have a Razor layout page which is used across my web application.
I have a code execution order issue. Normally, you set Page.Title (for example) on a sub page, and this variable is then populated by the time the Layout page is run so that you can place the title on your page in the Layout. That's fine. But I need to do it backwards. I would like to run some code for all pages, before code on sub pages executes.
I want to be able to run one single piece of code, that I don't have to reference in every single sub page, and I need it executed before the sub pages run any code. I'm sure there's a very simple and obvious way to do this that I'm missing as I'm new to the Razor Web Pages lifecycle...
Example: I set my site name in the Layout page. But for some reason I want to display the site name in a bunch of places in my sub pages, sub sub pages, etc. How would I best go about this?
I am aware of _PageStart.cshtml which I can use to run code before anything else on a folder by folder basis, set up Page.Whatever variables I may need etc., and this works for my purpose. But I'm having a hard time understanding if this is the normal process, or whether this is a bad approach. There doesn't seem to be too much on the web about this aspect of Razor Web Pages.
Upvotes: 2
Views: 767