how do you get the context path in the web app?

F

Flip

I'm trying to reference images in my webapp. I'm coming from j2ee and I
just write a small jsp scriplet that says to go grab the context path. How
do I do that with C#? If it's done differently in C#/.NET, please set me
straight (please be gentle :>).

If my current setup is this
+References
+Dilbert.jpg
Default.aspx
+Searching
+Default.aspx

In the /Searching/Default.aspx, I know I can reference the Dilbert.jpg image
using relative references
.../References/Dilbert.jpg

But how do I do it with absolute references? When I try this
/References/Dilbert.jpg
It is using the References as the webapp name. How can I pull the context
path (including port change if not default port 80)?

Thanks.
 
M

Matt Berther

Hello Flip,

Use the ~ in your server controls. ~ or Page.ResolveUrl("~/References/Dilbert.jpg")
will work from any level in your heirarchy. The ~ translates to the application
root.
 
F

Flip

Howdy Matt. Thank you for the reply.


Matt Berther said:
Hello Flip,
In case you were wondering, Flip's the nickname as my car was flipped over
Use the ~ in your server controls. ~ or Page.ResolveUrl("~/References/Dilbert.jpg")
will work from any level in your heirarchy. The ~ translates to the application
root.
When I read your email the first time, I thought the tilde was a mistype,
but when I got to your last sentence I realized you were serious! I have to
read up on that. Do you happen to have any MSDN links about this? If it's
easy to google, then no need for the MSDN link, I'll find it, but I didn't
have much luck with googling asp.net and "context path".

Thanks again! :>
 
M

Matt Berther

Hello Flip,

Glad you found it... ~ is one of the best things about ASP.NET. :=)
 
F

Flip

Glad you found it... ~ is one of the best things about ASP.NET. :=)
I just realized something when trying this out. The control has to be a
runat="server". Just a caveat if someone new is watching this thread.
 

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

Top