Last post Dec 11, 2018 02:41 PM by RageRiot
Dec 11, 2018 01:53 PM|RageRiot|LINK
I need to load CSS files based on the URL structure in to my Views/Shared/_Layout.cshtml view.
So, if the URL is mywebsite.com/sectiona/partb/page3?id=1234
I need to insert the following string to the bottom of my <head> section:
I'll obviously check the files exists before I add the string, but I have so many pages this is an easier solution that remembering to reference the CSS from each view page.
Where would I perform this? I'm learning MVC but there isn't a controller for the template - only views?
Dec 11, 2018 02:20 PM|mgebhard|LINK
One option is checking the route data.
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
if ((string)ViewContext.RouteData.Values["controller"] == "Home")
Another is dynamically injecting the CSS in the head similar to adding scripts from the View.
Dec 11, 2018 02:41 PM|RageRiot|LINK
Thank you mgebhard :)
I need this code to execute with every page so I've added a code block (my initial thought was this was bad practice).
In webforms, I'd just add this to the masterpage's pageload event.
Anyway, working now - thank you.