Convenient ~ (tilde) in path does not work in plain HTML?

P

PJ6

In the VB runtime I can specify paths from the base directory with ~ like
so-

Page.LoadControl("~\Controls\SomeControl.ascx")

but when rendering the LINK tag HTML at runtime, this doesn't work -

<LINK rel="stylesheet" type="text/css" href="~\Styles\Styles.css">

What's the syntax for getting the base directory in HTML, or - much less
desirable - do I have to use a relative path from the page location?

Paul
 
T

Tim_Mac

hi Paul,
you're using backslashes... these are for local paths only. some browsers
and frameworks might let you away with it though.
it should work if you use forward slashes. a link href is just like any
other web request, so the syntax should work regardless of request type:
html page, gif image, css stylesheet etc.

hyh
tim
 
T

tdavisjr

Use the ../Styles/Styles.css synthax. The ../ says to go up one
directory level. You can do ../../ to go up 2 directory levels from
where the page is located.
 
L

Lucas Tam

What's the syntax for getting the base directory in HTML, or - much less
desirable - do I have to use a relative path from the page location?

Tilde is processed by the server to find the base path.

Unfortunately I don't think there is a method to specify the base
directory, you'll need to use relative paths.

Well one alternative is to use all ASP.NET controls - thus bypassing plain
HTML. There will be more processing overhead, but I'm not sure how much.
 
G

George

Hi,

The problem is that "~" is replaced by ASP.NET engine.
Your HTML is not going through the ASP.NET as it does not even try to interpret what you have wrote if there is no runat="server".

The solution my depends on you problem.

1. If you are working on the website that suppose to work of the ROOT and you are developing in the folder. I can see why you do not want to hardcode the project path. And
My article on codeproject might help you with it
http://www.codeproject.com/aspnet/Multisite.asp

2. You can write the Request.Filter which will convert all "~" to the project path. But then be careful about not having "~" that is suppose to be "~".


George


In the VB runtime I can specify paths from the base directory with ~ like
so-

Page.LoadControl("~\Controls\SomeControl.ascx")

but when rendering the LINK tag HTML at runtime, this doesn't work -

<LINK rel="stylesheet" type="text/css" href="~\Styles\Styles.css">

What's the syntax for getting the base directory in HTML, or - much less
desirable - do I have to use a relative path from the page location?

Paul
 
P

PJ6

Bah then what is the equivalant to IO.Path.Combine? Guess I'll just have to
use string Replace.

BTW, ~ doesn't work at all client-side even using forward-slash.

Paul
 
P

PJ6

OK this works -

<LINK rel="stylesheet" type="text/css" href="/TestWebApp/Styles/Styles.css">

So how would I get root path and name IIS is using to serve the project
(which may be a different name than what it's called in VS)? I can probably
make the assumption if I have to though.

Thanks for your help,
Paul

The problem is that "~" is replaced by ASP.NET engine.
Your HTML is not going through the ASP.NET as it does not even try to
interpret what you have wrote if there is no runat="server".

The solution my depends on you problem.

1. If you are working on the website that suppose to work of the ROOT and
you are developing in the folder. I can see why you do not want to hardcode
the project path. And
My article on codeproject might help you with it
http://www.codeproject.com/aspnet/Multisite.asp

2. You can write the Request.Filter which will convert all "~" to the
project path. But then be careful about not having "~" that is suppose to be
"~".
 
J

Juan T. Llibre

re:
BTW, ~ doesn't work at all client-side

It's not supposed to.

Client-side is client-side and server-side is server-side,
and never the twain shall meet.

You can send server-side stuff client-side,
but sending client-side stuff server-side will take some contortions.
 
G

George

you can always use Server.MapPath("~") to ger the real path, but then you will need to write a code that would put it into HREF of the <LINK> tag

George


OK this works -

<LINK rel="stylesheet" type="text/css" href="/TestWebApp/Styles/Styles.css">

So how would I get root path and name IIS is using to serve the project
(which may be a different name than what it's called in VS)? I can probably
make the assumption if I have to though.

Thanks for your help,
Paul

The problem is that "~" is replaced by ASP.NET engine.
Your HTML is not going through the ASP.NET as it does not even try to
interpret what you have wrote if there is no runat="server".

The solution my depends on you problem.

1. If you are working on the website that suppose to work of the ROOT and
you are developing in the folder. I can see why you do not want to hardcode
the project path. And
My article on codeproject might help you with it
http://www.codeproject.com/aspnet/Multisite.asp

2. You can write the Request.Filter which will convert all "~" to the
project path. But then be careful about not having "~" that is suppose to be
"~".
 
L

Lucas Tam

<LINK rel="stylesheet" type="text/css"
href="/TestWebApp/Styles/Styles.css">

So how would I get root path and name IIS is using to serve the
project (which may be a different name than what it's called in VS)? I
can probably make the assumption if I have to though.

Thanks for your help,
Paul

Can you set the path in code?

Add a RunAt=Server to the Link - then set the StyleSheet path in the
codebehind?
 
P

PJ6

Server.MapPath("~")

Exactly what I was looking for.

Thanks,
Paul

you can always use Server.MapPath("~") to ger the real path, but then you
will need to write a code that would put it into HREF of the <LINK> tag

George
 

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,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top