Solved

How do I find the correct (maybe 'absolute'?) path to my pages?

Posted on 2014-10-29
4
420 Views
Last Modified: 2014-11-04
I've got my site (VS ASP .NET 4.0 C#) working on my local machine and my test server. I'm using some code that I found here:

http://stackoverflow.com/questions/7148872/asp-net-problems-with-static-selected-style-for-a-selected-page-on-the-menu

to highlight the menu tabs of my <asp:Menu> control. To find the correct path to my files I have to use two different "lengths" (using the .IndexOf and .LastIndexOf string functions) for some reason depending on if I'm looking locally for the path to my pages vs. me looking for the path on my test server. I understand why there is a difference in the paths but isn't there a way to find this path that will work no matter where I'm looking for my page(s) file path(s)?

here is the routine C# on my machine:

    protected void Page_Load(object sender, EventArgs e)
    {
        int firstIndex = 0;
        string path = "";

        if (Request.AppRelativeCurrentExecutionFilePath.ToString().Contains("/"))
        {
            firstIndex = Request.AppRelativeCurrentExecutionFilePath.ToString().IndexOf("/");
            path = Request.AppRelativeCurrentExecutionFilePath.ToString().Substring(firstIndex + 1, Request.AppRelativeCurrentExecutionFilePath.ToString().Length - 2);
        }
        else
            path = Request.AppRelativeCurrentExecutionFilePath;

        lblUserName.Text = path;
        foreach (MenuItem item in NavigationMenu.Items)
        {
            item.Selected = item.NavigateUrl.Equals(path, StringComparison.InvariantCultureIgnoreCase);
        }

        firstIndex = Page.User.Identity.Name.ToString().IndexOf("\\");
        lblUserName.Text = Page.User.Identity.Name.ToString().Substring(firstIndex + 1, Page.User.Identity.Name.ToString().Length - firstIndex - 1);
    }

Open in new window


here is the routine on my test server:

    protected void Page_Load(object sender, EventArgs e)
    {
        int lastIndex = 0;
        string path = "";

        if (Request.AppRelativeCurrentExecutionFilePath.ToString().Contains("/"))
        {
            lastIndex = Request.AppRelativeCurrentExecutionFilePath.ToString().LastIndexOf("/");
            path = Request.AppRelativeCurrentExecutionFilePath.ToString().Substring(lastIndex + 1, Request.AppRelativeCurrentExecutionFilePath.ToString().Length - 7);
        }
        else
            path = Request.AppRelativeCurrentExecutionFilePath;

        foreach (MenuItem item in NavigationMenu.Items)
        {
            item.Selected = item.NavigateUrl.Equals(path, StringComparison.InvariantCultureIgnoreCase);
        }

        lastIndex = Page.User.Identity.Name.ToString().IndexOf("\\");
        lblUserName.Text = Page.User.Identity.Name;//.ToString().Substring(lastIndex + 1, Page.User.Identity.Name.ToString().Length - lastIndex - 1);
    }

Open in new window


its not a lot of code, but to pin-point the issue, the code differs here:

my local code:
path = Request.AppRelativeCurrentExecutionFilePath.ToString().Substring(lastIndex + 1, Request.AppRelativeCurrentExecutionFilePath.ToString().Length - 2);

my code on the test server
path = Request.AppRelativeCurrentExecutionFilePath.ToString().Substring(lastIndex + 1, Request.AppRelativeCurrentExecutionFilePath.ToString().Length - 7);
0
Comment
Question by:mikesExpertExchange
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40412645
http://msdn.microsoft.com/en-us/library/system.web.httprequest.applicationpath%28v=vs.110%29.aspx

You are using AppRelativeCurrentExecutionFilePath but there is also an ApplicationPath, is that what you require?
0
 
LVL 29

Accepted Solution

by:
Pravin Asar earned 500 total points
ID: 40414905
0
 
LVL 1

Author Comment

by:mikesExpertExchange
ID: 40419438
I will try one or both of these solutions today.
0
 
LVL 1

Author Closing Comment

by:mikesExpertExchange
ID: 40422448
Thank you.
0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question