Root Relative Path Injected as String Variable

J

Jordan Richard

Put another way, is there any way I can tell ASP.NET to convert a path
(imbedded in a string variable, "~/images/some_image.gif") to a
root-relative path, that the client will understand, for the *current* page
request?

Here's what I'm doing: I am injecting HTML directly into ASPX pages via
LiteralControl.

something like this...

string myHTMLString =
"<table><tr><td>~/images/some_image.gif</td></tr></table>
litMyLiteral.Text = myHTMLString;

Notice that there is a path to an image in that HTML Table.

As shown, that path to the image obviously won't work because browsers
don't resolve "~" to the root, and ASP.NET doesn't get the opportunity to
convert that to a root-relative path that the client will understand because
it's just a string value from ASP.NET's point of view.
So, how can I have specify the root relative path correctly when injecting a
path (as a string) into a Literal - WHERE that Literal control may be on
ASPX pages in a variety of locations below the site root (i.e, the aspx
pages cannot be assumed to always exist at the root or any level below the
root)?

Thanks.
 
K

Karl Seguin

Do a search and replace?

litMyLiteral.Text = myHtmlString.Replace("~", Request.ApplicationPath);

or create a custom server control that does it for you..

public class MyLiteral : Literal{
protected override void Render(HtmlTextWriter writer){
if (Text != null){
Text = Text.Replace("~", REquest.applicationPath);
base.Render(writer)
}
}
}

or sumtin..

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 
B

Brock Allen

You'll need to call Control.ResolveUrl on the "~/images/foo.jpg" piece first.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top