how to find the website root physical path

Discussion in 'ASP .Net' started by AAaron123, Mar 30, 2009.

  1. AAaron123

    AAaron123 Guest

    I've been playing around with physical paths.

    I need to know how to find the website root physical path.

    I tried various things and find I can get the path to my Projects folder, to
    the folder the current page is in various other things but I'm never sure
    any one thing will work in all cases.

    Suppose this is the site

    webSiteFolder
    Default.aspx
    subFolder1
    page1.aspx
    subFolder2
    page2.aspx

    Suppose the site is first entered by specifying /subFolder/page1.aspx rather
    than /Default.aspx as it usually done (maybe this is not relevant).

    Then, in a .vb file, no matter which page I'm in

    How do I find the directory path to Default.aspx?
    That is, to the web root folder.

    If I'm in page2 how would you specify page1.aspx?

    What is the difference between the Application folder and the web root
    folder?
    I think I know what the web root is but what is the Application root?


    Thanks in advance if you wade through the above and answer some of it!

    PS
    I find the web root now by searching upward until I find Default.aspx.
    Kludgy?
     
    AAaron123, Mar 30, 2009
    #1
    1. Advertisements

  2. AAaron123

    Scott M. Guest

    There's no need to know the physical path, nor should you use it even if you
    knew it. You should ALWAYS be using relative paths within your site.

    You can always specify the root of the site by simply adding a forward-slash
    (/) to any relative reference.

    So, to get to your default page, you would simply need a path of
    "/default.aspx".

    If you were on page2 and needed a reference to page1, you'd do it relatively
    by indicating you need to go up one directory (to the parent folder of
    subfolder2) by using "../" and then indicating that you need to go into
    subfolder1 and finally page1: "../subfolder1/page1.aspx"

    The reason you don't want to use physcial paths is that they can change over
    time and between development and production servers. Relative paths should
    stay consistent.

    -Scott
     
    Scott M., Mar 30, 2009
    #2
    1. Advertisements

  3. AAaron123

    imransyed63

    Joined:
    May 16, 2006
    Messages:
    27
    Likes Received:
    0
    Request.ApplicationPath will give you the root path of your website,
    as per your example,
    Request.ApplicationPath will return you webSiteFolder no matter on what page you are.
     
    imransyed63, Mar 30, 2009
    #3
  4. AAaron123

    psycho Guest

    You can use Server.MapPath() to get the physical path of current page
    being processed.
     
    psycho, Mar 30, 2009
    #4
  5. Web (domain) root is a folder that corresponds to domain of the
    website created in IIS. Example: by default IIS has a sample website
    in the C:\Inetpub\wwwroot folder. This folder is a web root of http://localhost

    Application root is a folder that corresponds to URL of application.
    Typically, it the same as web domain root, but if you have http://localhost/app1,
    http://localhost/app2 then your application root is location of the
    path to ASP.NET application (e.g. c:\inetpub\wwwroot
    \VirtualDirectory1). The application root usually contains some
    specific files: web.config, bin-folder, etc.
     
    Alexey Smirnov, Mar 30, 2009
    #5
  6. AAaron123

    AAaron123 Guest

    I need to pass the physical path to SQLServer 2008 to create a database in a
    specified folder.

    It works OK if I use the long newFolder string I show below.

    But not if I add the newFolder = "/" statement.

    Can you figure a way to do that?



    Dim newFolder As String = "I:\My Documents\Visual Studio
    2008\WebSites\MySite\App_Data\"

    newFolder = "/"


    Thanks
     
    AAaron123, Mar 30, 2009
    #6
  7. AAaron123

    AAaron123 Guest

    Please see my reply to Scott M.

    thanks
     
    AAaron123, Mar 30, 2009
    #7
  8. AAaron123

    AAaron123 Guest

    Thanks for your extensive comments below.
    I'm not confident I really understand so I wrote some stuff below hoping
    you'll indicate agreement or disagreement.
    I know it's really too much to expect you to wade through but I can hope!

    If I open IIS on my machine and expand I see:
    My machine name(local computer)
    Web Sites
    Default Web Site
    many folder names including the what is my application root if I
    understand your note below (because it contains web.config, bin-folder,
    etc.)
    Properties show the path to be
    I:\My Documents\Visual Studio 2008\WebSites\MySite
    so this isthe application folder for my site


    I used IE to open http://localhost
    and got to
    http://localhost/localstart.asp
    I find that file in
    C:\Inetpub\wwwroot

    So is C:\Inetpub\wwwroot a web root folder
    Is there an Application frolder that goes with this?

    When I run my site I see
    http://localhost:1350/MySite/Default.aspx
    so is /MySite my web root folder

    In this case the web and root folder are the same.
    But they need not be?

    If they are not how can I get vb to give me the string
    I:\My Documents\Visual Studio 2008\WebSites\MySite\App_Data

    Thanks a lot



    Web (domain) root is a folder that corresponds to domain of the
    website created in IIS. Example: by default IIS has a sample website
    in the C:\Inetpub\wwwroot folder. This folder is a web root of
    http://localhost

    Application root is a folder that corresponds to URL of application.
    Typically, it the same as web domain root, but if you have
    http://localhost/app1,
    http://localhost/app2 then your application root is location of the
    path to ASP.NET application (e.g. c:\inetpub\wwwroot
    \VirtualDirectory1). The application root usually contains some
    specific files: web.config, bin-folder, etc.
     
    AAaron123, Mar 30, 2009
    #8
  9. The easiest way to find the website root application's
    physical path is with Server.MapPath(Request.ApplicationPath).

    Server.MapPath(Request.ApplicationPath) gives you the string you need to pass the
    application's physical path to SQLServer 2008 to create a database in a specified folder.




    Juan T. Llibre, asp.net MVP
    asp.net faq : http://asp.net.do/faq/
    =========================
     
    Juan T. Llibre, Mar 30, 2009
    #9
  10. Aaron,

    In your example,

    C:\Inetpub\wwwroot is the web (domain) root folder pointed to http://localhost
    I:\My Documents\Visual Studio 2008\WebSites\MySite is the application
    root of http://localhost:1350/MySite/

    The port number of 1350 tells me that you are using ASP.NET
    development server from VS.NET, and this is maybe why you were
    confused by difference in localhost vs. localhost:1350. To make this
    more clear you just need to know following thing: a typical web
    application is a set of web pages (web forms), other files and
    directories, linked to each other. A website could have one or more
    web applications. Go to IIS, open a website properties, go to Home
    Directory tab. You will see an area with Application settings.
    Navigate to a any subdirectory in your website in IIS, again open
    Properties - Directory. You will see again Application settings. If
    settings are grayed out, subdirectory is a part of the parent
    application. Once you clicked Create you will mark that directory as a
    new web application.

    To get I:\My Documents\Visual Studio 2008\WebSites\MySite\App_Data use

    Server.MapPath("~/App_Data")
     
    Alexey Smirnov, Mar 30, 2009
    #10
  11. AAaron123

    AAaron123 Guest

    Thanks
     
    AAaron123, Mar 30, 2009
    #11
  12. AAaron123

    AAaron123 Guest

    That great!

    Thanks a lot.

    Aaron,

    In your example,

    C:\Inetpub\wwwroot is the web (domain) root folder pointed to
    http://localhost
    I:\My Documents\Visual Studio 2008\WebSites\MySite is the application
    root of http://localhost:1350/MySite/

    The port number of 1350 tells me that you are using ASP.NET
    development server from VS.NET, and this is maybe why you were
    confused by difference in localhost vs. localhost:1350. To make this
    more clear you just need to know following thing: a typical web
    application is a set of web pages (web forms), other files and
    directories, linked to each other. A website could have one or more
    web applications. Go to IIS, open a website properties, go to Home
    Directory tab. You will see an area with Application settings.
    Navigate to a any subdirectory in your website in IIS, again open
    Properties - Directory. You will see again Application settings. If
    settings are grayed out, subdirectory is a part of the parent
    application. Once you clicked Create you will mark that directory as a
    new web application.

    To get I:\My Documents\Visual Studio 2008\WebSites\MySite\App_Data use

    Server.MapPath("~/App_Data")
     
    AAaron123, Mar 30, 2009
    #12
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.