'Trying to get the routeName but it's always empty

I tried to get the routeName from the URL because i need to set another class in the Layout of the body if i'm on the /Category page.

enter image description here

  @{string classContent = Request.QueryString["routeName"] != "/Category" ? "container" : "";}; 
  <div id="Content" class="body-wrapper @classContent">

My problem is, Request.QueryString["routeName"] is always empty and couldn't find why.

Does someone know why it's always empty or has a better approach for setting a different class if you're on a certain page?



Solution 1:[1]

In the end i solved it with that code:

   var segments = Request.Url.AbsolutePath.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
   string classContent = "container";
   if (segments.Count() > 1) { classContent = segments[1] != "category" ? "" : "container";}

Request.Url.AbsolutePath gets the whole URL.

enter image description here

After that i split the whole URL and save it into a list.

Then i test if the list is long enough to be on another site except home.

In the end i look if the second part of the url is /Category and save the Css class appropriate to the output of the url.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1