Wrox Programmer Forums
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 16th, 2003, 07:56 PM
Authorized User
 
Join Date: Aug 2003
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default Links User Control

I have a problem with using user controls for common page elements such as navigation bars. If I use the control in subfolders of the site, as is necessary for a large website, the links don't work. This can be worked around by using root-relative paths, e.g. instead of "Images/Navbar.gif" using "/MySite/Images/Navbar.gif".

However, this then creates a deployment problem. Let's say I want to deploy the site on a host, e.g. www.mysite.com, I don't want it to have to be www.mysite.com/MySite in order for the links to work!

Is there any way of working around this, which allows the links to work properly in the Visual Studio Web Forms designer?

Any help is much appreciated.

 
Old September 17th, 2003, 08:33 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Indeed there is a very easy fix for this. ASP.Net has eliminated a major problem from ASP that I'm sure many people have had when building sites with the MS Visual tools which inevitably have a subdirectory where the project exists and a corresponding virtual directory in IIS. (In ASP we had to write complicated functions to deal with this for us.)

They have taken the hint from the unix world and implemented "~" as a sort of "home" placeholder character.

This functionality appears to be built into the server controls that need a virtual/relative path to resources (images, usercontrols, links, etc) If you are using one of these server controls you need only to define the resource path like this...

    "~/mypage.aspx"

Make your resource path attribute start with "~/" (establishing the root of the project) then just provide the full path within the hierachy of your project.

    "~/images/someimage.gif"
    "~/afolder/somepage.aspx"

These references will work from anywhere within your application because it will replace "~" with the application root if there is any. So the items above become:

    "/myWebApplication/mypage.aspx"
    "/myWebApplication/images/someimage.gif"
    "/myWebApplication/afolder/somepage.aspx"

Furthermore, you could even put user controls in a folder for organization. When you refer to the controls source page you just do the same thing:

In an ASPX file using the tag registration directive:
<%@ Register TagPrefix="uc" Tagname="myUserControl" Src="~/UserControlFolder/myUserControl.ascx" %>

    or

In code-behind:
LoadControl("~/UserControlFolder/myUserControl.ascx")

Unfortunately, if you want to use this functionality in static HTML you run into a little problem as there is no way to do the replacement of "~". But... there is a method off of the Control object (from which Page and UserControl are derived) called "ResolveURL". You can call this with the same formatted string ("~/...") and it will resolve it to the application root. I believe (no guarantees) that you could even do this within an ASPX (do it ASP style):

<a href="<%=ResolveURL("~/folder/page.aspx")%>">My Link</a>

Fortunately, none of this is a workaround, it's built in but poorly documented. I came across is looking at someone's code regarding something totally unrelated. I recognized what it was from my limited *nix knowledge and tried it out. Now I am in the habit of using it consistently throughout my applications so there's never a possibility of broken resource references.

Peter





Similar Threads
Thread Thread Starter Forum Replies Last Post
Disable side bar links of Wizard Control everest ASP.NET 2.0 Professional 0 February 14th, 2007 01:33 AM
Add Windows User control in Web User Control agarwalvidhu C# 0 March 30th, 2006 01:17 AM
Help! Custom Server Control using User Control diehard ASP.NET 1.0 and 1.1 Professional 2 January 4th, 2006 12:33 PM
Help with control initialization in user control mike_remember ASP.NET 1.0 and 1.1 Professional 7 December 19th, 2005 11:08 AM
'Member Links' Control stu9820 ASP.NET 1.0 and 1.1 Basics 6 May 27th, 2004 11:49 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.