SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ASP.NET Paths Question

    Hi,

    I am fairly new to .NET and am working on a pretty basic web site. The problem I am having, is I don't understand how the whole pathing system works. Specifically, I have my project running at http://localhost/foo/, but when I copy the files to the web root (http://localhost/), all the application paths (Request.ApplicationPath) are set to "/" which causes file references to be "//foo.js". How is this supposed to work when deploying to the web root? Is there something I am missing?

    Please help!
    Last edited by idledaylight; Aug 7, 2007 at 13:26.

  2. #2
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found a solution. Use ResolveUrl("~/foo/foo.gif") instead of Request.ApplicationPath. Would that be a best practices way of doing things?

  3. #3
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Yes, ResolveUrl is next to godliness. Though you should use it relative to the root of your application. Ie, if foo.gif is in the root, you should call foo like so:

    Code:
    <img src='<&#37;= ResolveUrl("~/foo.gif") %>' alt="Foo" />

  4. #4
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is it a bad idea to make all links/images use either the ResolveUrl() or ~ method for performance concerns? Or are you still "supposed" to build the site for deployment in either the root "/" or a directory "/foo/"...?

  5. #5
    SitePoint Wizard
    Join Date
    Feb 2007
    Posts
    1,274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's no need to make all links go through ResolveUrl or make all links root relative (~/). If the url is truly relative, just use a relative path.

    ResolveUrl helps you deploy/test an application in virtual directories etc because it resolves the ~/ to the application root. You should use it where you would otherwise make a root relative path. E.g. "/Design/arrow.gif" should be written as <&#37;=ResolveUrl("~/Design/arrow.gif") %>

  6. #6
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    What that man said. The big tricks come in when you are dealing with User Controls, which can sometimes futz with URLs. My general simplified rule is start with a normal relative hyperlink and then resort to using ASP.NET to parse things if that fails.

  7. #7
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the feedback! I am more concerned with image references than with hyperlinks. Before working with .NET, I usually referenced most (often all) images with absolute paths (/images/foo.gif). So really my question is, should I try to use relative (../images/foo.gif) and absolute (/images/foo.gif) file references over the .NET root relative paths (~/images/foo.gif) for performance reasons? Or is that really what the feature is there for...to be used?

    Also, slightly unrelated: are there any (performance or otherwise) differences between using a <asp:Image /> tag and an <img runat="server" /> tag? Or are they identical in the eyes of the .NET Web server?

  8. #8
    SitePoint Guru Chroniclemaster1's Avatar
    Join Date
    Jun 2007
    Location
    San Diego, CA
    Posts
    784
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    let me throw in one more, what about using an <img> tag too. How does that compare for performance, maintenance, and best practices?
    Whatever you can do or dream you can, begin it.
    Boldness has genius, power and magic in it. Begin it now.

    Chroniclemaster1, Founder of Earth Chronicle
    A Growing History of our Planet, by our Planet, for our Planet.

  9. #9
    SitePoint Wizard
    Join Date
    Feb 2007
    Posts
    1,274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Non server side tags are faster than server side tags. Plain <img> tags are faster than both <asp:Image> and <img runat="server">. Indeed all adjacent non-server side tags are regarded as one "literal" control which will simple render the static text.

    Each server side tag will result in a node in the control tree. Controls have a "rich" lifecycle so it not only hooks into rendering but also other phases such as loading.

    However, unless such a control is instantiated inside a loop of several 100s it is not going to be an issue.

    Using a code rendering block such as <&#37;= ResolveUrl("") %> will *not* create a server side control. It will only result in code being executed during rendering, and will be faster than a server side control. Btw, ResolveUrl is pretty fast, it merely replaces ~ with the application path.
    Last edited by honeymonster; Aug 8, 2007 at 01:53.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •