[Help] Url rewriting and Images ref

F

Fransis il Mulo

I wrote code to manage Urlrewriting in Application_BeginRequest:

if (!System.IO.File.Exists(Request.PhysicalPath))
{
string sRequestedUrl = Request.Path;
string sTargetUrl = GetRurl(sRequestedUrl);
Context.RewritePath(sTargetUrl, false);
}

GetRurl map real url on virtual url.

I get a problem with images ref. In the root dir i have a subdir called
images and in html code I have ref like these:

<img src="images/img.jpg">
or
<body background="images/back.jpg">

When the code transforms shop/books/one.html in content.aspx?id=123456 I
obtain an exception due to wrong path (shop/books/images/img.jpg instead
of images/img.jpg). If a use ref like ~/images/img.jpg I still obtain
wrong Request.PhysicalPath...

The url of my application is http://MySite/MyApp.


Can you help me?


Bye

Fransis
 
F

Fransis il Mulo

I try to explain:

In my page i've two tag like these:


<link href="~/styles/style.css" rel="stylesheet" type="text/css" />

and

<body background="~/img/image.jpg" topmargin=0 bottommargin=0>

the property Request.PhysicalPath return a right value

"c:\\inetpub\\wwwroot\\myApp\\styles\\style.css" and a wrong value

"c:\\inetpub\\wwwroot\\myApp\\~\\img\\image.jpg".

Why? Can you help me?
 
B

bruce barker

the ~path is evaluated at render time only for server controls (those with a
runat=server). what confusing is that the <head> (and any child <link>s) s a
server control. the <body> is not a server control automatically.

if you us a ~path on a non-server control, its rendered as ~path, and thats
what the browser will send.

-- bruce (sqlwork.com)
 
B

bruce barker

you need to set the path relative to what the browser sees. as you are using
url rewriting you are tricking .net

take url

http://mysite/myapp/mypage/fake.htm

which is rewritten

http://mysite/myapp/mypage.aspx?id=fake

now if you use ~/images/image.gif, .net determines the current request is at
the base dir so it renders:

src="images/image.gif"

but the browser sees the before url. so when it requests the image it sends:

http://mysite/myapp/mypage/images/image.gif

because to it the base dir is "http://mysite/myapp/mypage"

you need to url the image requests also, calc the path in your code.

-- bruce (sqlwork.com)
 

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

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,698
Latest member
LydiaHalle

Latest Threads

Top