Q:

What is the best way to pass a variable to a partial in the layout about the current page?

I've implemented a huge template to a new .Net Core MVC app and have separated it to various partials (header, sidebar, footer).
It works well - however, the side menu bar is meant to have
<li class="active">

on the currently visited page and change from:
<li class="treeview">

to 
<li class="treeview active menu-open">

on any open section.
Other than loads of if statements or a lot of VERY messy code, I can't figure out how to do this. Can someone point me in the correct direction please?

A:

You don't need if statements, but you will need to employ ternaries (i.e. some amount of conditional logic is required). The most terse way I've found to handle this sort of thing is with code along the lines of:
@{ string url; /* define anywhere, just needs to be before nav HTML */ } 

...

@{ url = Url.Action("Foo"); }
<li class="@(Request.Path == url ? "active" : null)">
    <a href="@url">Foo</a>
</li>
@{ url = Url.Action("Bar"); }
<li class="@(Request.Path == url ? "active" : null)">
    <a href="@url">Bar</a>
</li>

The url variable is declared first just so 50,000 different variables aren't necessary. The URL is saved to a variable at all simply so you do not need to repeat any calls to Url.Action and such (i.e. one for the conditional and another for the actual link). Here, I'm just doing a direct comparison match, but you may want the list item marked active even if the current page is a sub item. For that you'd simply use Request.Path.StartsWith(url) instead as your conditional. However, don't use that on the link to your homepage (i.e. /) or it will always be marked active.
Follow the same approach with your "treeview" items.
@{ url = Url.Action("Item1"); }
<li class="treeview @(Request.Path == url ? "active menu-open" : null)">
    <a href="@url">Tree view item</a>
</li>

