?
Solved

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

Posted on 2014-10-29
4
Medium Priority
?
421 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:Michael Sterling
[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 2000 total points
ID: 40414905
0
 
LVL 1

Author Comment

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

Author Closing Comment

by:Michael Sterling
ID: 40422448
Thank you.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

777 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