7

I am using code like this to detect the browser and apply a condition to display or not display <label>:

@{
var settings = Model.PartFieldDefinition.Settings.GetModel();
System.Web.HttpBrowserCapabilitiesBase theBrow = Request.Browser;
}
<fieldset>
@{
   if ((theBrow.Browser == "IE") && (theBrow.MajorVersion < 10)) 
   {
       @:<label>@Model.DisplayName</label>
       @://Some Code for inputs
   }
   else 
   {
        @://Some Code for inputs
   }
}
</fieldset>

I have been trying to figure out if I can (and if so, how) I could utilize this logic to detect what path in the website the user is in to display or not display <label>

For example, on any other path I want the label displayed. However, for the pages under ~/Services (the path), I want to be able to hide the label.

It's a little bit more complicated than that. The code is in an editor template that gets used all over the site. If I use CSS in the editor template to "hide" the label I will affect all the pages. I was hoping with the above logic I can avoid that and only apply my logic on certain pages under a path.

Thanks.

Update I have tried the following:

@{
    string CurrentURL = Request.ApplicationPath;
}
<fieldset>
@{
    if ((CurrentURL == "Services")) // also tried "/Services"
    // rest of code left out for brevity
}

but the labels are hidden on all pages, even outside the path "~/Services". Am stuck on the syntax.

Found answer

I did the following:

@{
    string CurrentURL = Request.Url.AbsoluteUri;
}
<fieldset>
@{
    if (CurrentURL.Contains("Services"))
}

and it worked. I don't know if anyone can comment, but which would be better to use: Request.Url.AbsoluteUri or Request.Url.ToString() (both of which work)?

REMESQ
  • 1,190
  • 2
  • 26
  • 60
  • 1
    Is this what you are looking for : http://stackoverflow.com/a/5304805/2219239 ? – hemma731 Nov 05 '13 at 09:56
  • 1
    @hemma731 Thanks, but I cannot seem to get the syntax down properly. I want to hide labels down a path and all pages under it. So, if I want to do that with all pages under "~/Services" what might be the proper syntax? – REMESQ Nov 05 '13 at 15:41
  • 1
    If you try to target pages under "/services", can I assume they are all handled in the same controller ServicesController, or an area ServicesArea ? In this case, you could test your route values with if(HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString().Equals("Services"))... Is this helping you out ? – hemma731 Nov 05 '13 at 17:16

0 Answers0