locked
what is the correct format for setting a path for navigation RRS feed

  • Question

  • User-1106011126 posted

    I have as an option, "~/" to mean the root of a website.  For some reason, it has not worked successfully in using it in a hyperlink path, or some other object requiring a location of a file or program.  I have made these paths work by using an absolute path of the file system when I am developing on a local server/browser, but obviously this will not work when I deploy the site.  Any insight or solution will be appreciated.

    Friday, August 1, 2014 4:29 PM

Answers

  • User1428246847 posted

    eps123

    I have as an option, "~/" to mean the root of a website.  For some reason, it has not worked successfully in using it in a hyperlink path, or some other object requiring a location of a file or program.  I have made these paths work by using an absolute path of the file system when I am developing on a local server/browser, but obviously this will not work when I deploy the site.  Any insight or solution will be appreciated.

    For hyperlinks, the root of the website is "/", not "~/". So "/mypage.aspx" in a hyperlink will navigate to the page mypage.aspx in the webroot. "~/mypage.aspx" will navigate to the page mypage.aspx in a subdirectory called '~' of the current directory.

    For file paths, you have to map the web path to an operating system path. The (C#) code below shows how

    string pathnew = "/SDATA";  // or "~/SDATA"
    string p = Server.MapPath(pathnew);
    

    The result on my development machine:

    F:\\3_DevelopmentDemo\\WebApplication1\\WebApplication1\\SDATA
    

    PS

    Sorry for posting C# code, but I can only read VB, not write ;)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 13, 2014 12:25 AM

All replies

  • User-1360095595 posted

    Can you show how you tried to use it in a hyperlink? 

    Friday, August 1, 2014 4:44 PM
  • User-581111879 posted

    Try using this this.ResolveUrl("~/images/myImage.png")

    so on your hyperlink path, bind the href property using this or you can dynamically create the hyperlink and set the href property through code behind.

    Friday, August 1, 2014 4:48 PM
  • User-271186128 posted

    Hi eps123,

    In many cases, elements or controls on your page must refer to an external resource such as a file. ASP.NET supports various methods for referencing external resources. The reference method you choose depends on whether you are working with a client-side element or a Web server control. Here are some tutorials about ASP.NET Web Site Paths, perhaps they can help you.

    ASP.NET Web Site Paths: http://msdn.microsoft.com/en-us/library/ms178116(v=vs.90).aspx

    Control.ResolveUrl Method: http://msdn.microsoft.com/en-us/library/system.web.ui.control.resolveurl(v=vs.110).aspx

    Best Regards,
    Dillion

    Monday, August 4, 2014 6:07 AM
  • User-1106011126 posted

    Sorry for the long delay but I was experimenting.   Hyperlinks work fine when I am referencing a task that sits in the site root.  Could not figure out how to hyperlink from a code-behind file, so I am using "response.redirect" as an alternate.  The results are indicated below

    Protected Sub LOGIN_Click(sender As Object, e As EventArgs) Handles LOGIN.Click
            'launch a login processvia a remote call
            'At present, the login will write a message to a fixed location in my root directory which will be fetched after a wait
            'The message will indicate a user number or a failure.
            'This is not satisfactory if multiple users are logging in
            ' Response.Redirect(String.Format("YourTargetPage.aspx?A={0}&B={1}",valueA,valueB));
            'Response.Redirect(String.Format("~/LOGINW/LOGINW/default.aspx"))       'This was attempt 1
            '[HttpException (0x80004005): Cannot use a leading .. to exit above the top directory.]
            Response.Redirect(String.Format("LOGINW/LOGINW/default.aspx"))             'and this was attempt 2
            'Exception Details: System.Web.HttpException: Cannot use a leading .. to exit above the top directory.
                End Sub
    I am not trying to complicate things, but the multiple directories are introduced in the course of describing the necessary ingredients for VS2010.  I know that if I put my working page in

    my root, it would work, but figuring out what are the files that must be ported to the root is a big headache that I need to avoid.  The first "response line that is commented out was a solution provided to me for another purpose.  I hope I have not used the call incorrectly.  I will appreciate answers questions or comments.

    eps123

    Thursday, August 7, 2014 4:26 PM
  • User-271186128 posted

    Hi eps123,

    Cannot use a leading .. to exit above the top directory

    What this means is that your web page is referring to content which is in the folder one level up from your page, but your page is already in the website's root folder, so the relative path is invalid.

    According to your code and description, I have created a sample, and it worked well on my side. I created a new website, add a Login page, a Default page and a folder named "XYZ" in the root folder. And I also add a XYZ_Default page and a XYZ_Login page in the XYZ folder. You could refer to the following code for more details.

    Code in Login page

    .aspx
        <div>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        </div>
    
    .aspx.cs
        Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Response.Redirect(String.Format("~/XYZ/XYZ_Default.aspx")) 'worked well
            'Response.Redirect(String.Format("XYZ/XYZ_Default.aspx"))  'worked well
    
            'Response.Redirect(String.Format("Default.aspx"))     ' Default page in root folder   worked well
            'Response.Redirect(String.Format("../Default.aspx"))  'error Cannot use a leading .. to exit above the top directory. 
        End Sub

    And, I also tested in XYZ_Login page.

    Code in XYZ_Login Page:

    .aspx
        <div>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        </div>
    .aspx.cs
        Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'Response.Redirect(String.Format("~/XYZ/XYZ_Default.aspx")) 'worked well
            'Response.Redirect(String.Format("XYZ_Default.aspx"))  'worked well
            'Response.Redirect(String.Format("XYZ/XYZ_Default.aspx")) 'error:The resource cannot be found. Because there is no XYZ folder in the XYZ folder
            'Response.Redirect(String.Format("../XYZ/XYZ_Default.aspx")) 'Worked well
            'Response.Redirect(String.Format("../Default.aspx")) ' Worked well
            'Response.Redirect(String.Format("../../Default.aspx")) 'Cannot use a leading .. to exit above the top directory. 
        End Sub

    According to sample, it seems that the code your posted is no problem. I would like to suggest you check your project and your code, especially the url in you page, such as image url.

    Here is a tutorial about ASP.NET Web Project Paths: http://msdn.microsoft.com/en-us/library/ms178116(v=vs.100).aspx

    And, here is a similar thread: http://forums.asp.net/t/1019849.aspx

    Perhaps they can help you.

    Best Regards,
    Dillion

    Friday, August 8, 2014 1:25 AM
  • User-1106011126 posted

    Thank you so much for a very detailed response to my question.  I have tried to implement your suggestions, but I feel that I am chasing a different problem.  My unexpected problem seems to be related to my chdir function.  I've pasted in the server diagnostic below.  I should note that same code works perfectly when the path used is a file system path such as "C:/...."

    Any further help will be appreciated because I cannot proceed without a solution or a work-around method to accomplish the chdir.

    Server Error in '/' Application.


    Could not find a part of the path 'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\~\SDATA'.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\~\SDATA'.

    Source Error:

    Line 42:         Dim pathnew As String = "~/SDATA"
    Line 43:         pathnew = String.Format(pathnew)
    Line 44:         ChDir(pathnew)
    Line 45:         'ChDir(path)
    Line 46:         FileOpen(3, "MESSAGE", OpenMode.Output)


    Source File: C:\Users\EPSNEW\Documents\WebSite34 - Copy (2)\LOGINW\LOGINW\Default.aspx.vb    Line: 44

    Stack Trace:

    [DirectoryNotFoundException: Could not find a part of the path 'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\~\SDATA'.]
       System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +359
       System.IO.Directory.SetCurrentDirectory(String path) +10529634
       Microsoft.VisualBasic.FileSystem.ChDir(String Path) +104
       LOGINW._Default.LOGIN_Click(Object sender, EventArgs e) in C:\Users\EPSNEW\Documents\WebSite34 - Copy (2)\LOGINW\LOGINW\Default.aspx.vb:44
       System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9752490
       System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +196
       System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724
    
    Tuesday, August 12, 2014 8:51 AM
  • User1428246847 posted

    eps123

    I have as an option, "~/" to mean the root of a website.  For some reason, it has not worked successfully in using it in a hyperlink path, or some other object requiring a location of a file or program.  I have made these paths work by using an absolute path of the file system when I am developing on a local server/browser, but obviously this will not work when I deploy the site.  Any insight or solution will be appreciated.

    For hyperlinks, the root of the website is "/", not "~/". So "/mypage.aspx" in a hyperlink will navigate to the page mypage.aspx in the webroot. "~/mypage.aspx" will navigate to the page mypage.aspx in a subdirectory called '~' of the current directory.

    For file paths, you have to map the web path to an operating system path. The (C#) code below shows how

    string pathnew = "/SDATA";  // or "~/SDATA"
    string p = Server.MapPath(pathnew);
    

    The result on my development machine:

    F:\\3_DevelopmentDemo\\WebApplication1\\WebApplication1\\SDATA
    

    PS

    Sorry for posting C# code, but I can only read VB, not write ;)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 13, 2014 12:25 AM