Not displaying content by its URL string - absolute urls

71 Views Asked by At

Serious amateur here when it comes to C#. In .net I'm using a line of code to hide elements that I dont want to appear on a particular URL (below)

        <% if (!HttpContext.Current.Request.Url.AbsoluteUri.Contains("/level-1"))
           { %>

Content here

    <% } %>

How do I adjust the url string to hide elements on level-3 but not necessarily 2?...(below)

Contains("/level-1/.../level-3/")

I could just specify the absolute url for level-3 but there are hundreds of them, so looking for a way to exclude all urls under level-3

1

There are 1 best solutions below

4
On BEST ANSWER

To always hide content for any URL that has 3 levels, then the following will do the trick.

var uri = HttpContext.Current.Request.Url.AbsoluteUri;
var parts = uri.Split('/');

if (parts.Length < 6)
{
    // doesn't contain 3rd level
    // html here
}