Solved

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

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

829 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