Solved

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

Posted on 2014-10-29
4
325 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
  • 2
4 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
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 28

Accepted Solution

by:
Pravin Asar earned 500 total points
Comment Utility
0
 
LVL 1

Author Comment

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

Author Closing Comment

by:mikesExpertExchange
Comment Utility
Thank you.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now