Solved

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

Posted on 2014-10-29
4
416 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

749 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