By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,708 Members | 1,529 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,708 IT Pros & Developers. It's quick & easy.

URL Rewriting and file paths

P: n/a
Hi guys

I'm having trouble with URL rewriting using
HttpApplication.Context.RewritePath in a web application I've created.

Everything works, but the links (css, images) in the pages break when I do a
URL rewrite.

For example, my masterpage has css links in the header to
"~/_css/myfile.css". The masterpage is in a folder called "/_mp".

The files default.aspx and contactus.aspx (both in the root) work fine, and
display the masterpage with all the styles and images.

However, if I go through my url rewriter (say, by visiting
localhost:port/myweb/contactus/ - where i've mapped /contactus/ to go to
/contactus.aspx), then the page loads, but none of the images or CSS work.

Could someone provide me with a link or info on how URL rewriting works,
specifically in relation to css/images, and when using the built-in server in
VS. It didn't work at all until I set my rewrite path to
"/myweb/contactus.aspx", and it won't let me use "../" or "./" or "~/" - it
says that's outside the application?

Some help and understanding required, thanks!
Dan
Jul 26 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
You may want to give path for CSS and JS as relative path w.r.t root .
That is , /myweb/mycss.css
Note that by URL rewriting , u r just giving browser a diffeent
output . As far as the browser is concerned all the relative path
computation will be against the url initially requested for , in ur
case localhost:port/myweb/contactus/

Regards
Nirmal

On Jul 27, 2:46 am, musosdev <musosw...@community.nospamwrote:
Hi guys

I'm having trouble with URL rewriting using
HttpApplication.Context.RewritePath in a web application I've created.

Everything works, but the links (css, images) in the pages break when I do a
URL rewrite.

For example, my masterpage has css links in the header to
"~/_css/myfile.css". The masterpage is in a folder called "/_mp".

The files default.aspx and contactus.aspx (both in the root) work fine, and
display the masterpage with all the styles and images.

However, if I go through my url rewriter (say, by visiting
localhost:port/myweb/contactus/ - where i've mapped /contactus/ to go to
/contactus.aspx), then the page loads, but none of the images or CSS work.

Could someone provide me with a link or info on how URL rewriting works,
specifically in relation to css/images, and when using the built-in server in
VS. It didn't work at all until I set my rewrite path to
"/myweb/contactus.aspx", and it won't let me use "../" or "./" or "~/" - it
says that's outside the application?

Some help and understanding required, thanks!

Dan

Jul 27 '07 #2

P: n/a
Hi Dan,

If your CSS or image references are "rooted", then they should still work
fine. See Scottgu's article here:

#Tip/Trick: Url Rewriting with ASP.NET - ScottGu's Blog
http://weblogs.asp.net/scottgu/archi...l-rewriting-wi
th-asp-net.aspx
<quote>
Handling CSS and Image Reference Correctly

One gotcha that people sometime run into when using Url Rewriting for the
very first time is that they find that their image and CSS stylesheet
references sometimes seem to stop working. This is because they have
relative references to these files within their HTML pages - and when you
start to re-write URLs within an application you need to be aware that the
browser will often be requesting files in different logical hierarchy
levels than what is really stored on the server.

For example, if our /products.aspx page above had a relative reference to
"logo.jpg" in the .aspx page, but was requested via the
/products/books.aspx url, then the browser will send a request for
/products/logo.jpg instead of /logo.jpg when it renders the page. To
reference this file correctly, make sure you root qualify CSS and Image
references ("/style.css" instead of "style.css"). For ASP.NET controls,
you can also use the ~ syntax to reference files from the root of the
application (for example: <asp:image imageurl="~/images/logo.jpg"
runat="server"/>
</quote>
Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 27 '07 #3

P: n/a
Thank you to you both.

I used nirmals suggestion, which works fine for this project. (thanks!)

However, Walter.. I understand what you're saying about "rooting" the URLs,
but when I try adding "~/" to the stylesheet and image URLs in my masterpage,
everything breaks (no images, no styles).

Is this because when you run a project from the built-in server, it gives it
a path of "http://localhost:xxxxx/mysite/" ?

Can you stop it from launching it at "/mysite/" and just have it on
"http://localhost:xxxx/" ?!

Thanks,
Dan

"Walter Wang [MSFT]" wrote:
Hi Dan,

If your CSS or image references are "rooted", then they should still work
fine. See Scottgu's article here:

#Tip/Trick: Url Rewriting with ASP.NET - ScottGu's Blog
http://weblogs.asp.net/scottgu/archi...l-rewriting-wi
th-asp-net.aspx
<quote>
Handling CSS and Image Reference Correctly

One gotcha that people sometime run into when using Url Rewriting for the
very first time is that they find that their image and CSS stylesheet
references sometimes seem to stop working. This is because they have
relative references to these files within their HTML pages - and when you
start to re-write URLs within an application you need to be aware that the
browser will often be requesting files in different logical hierarchy
levels than what is really stored on the server.

For example, if our /products.aspx page above had a relative reference to
"logo.jpg" in the .aspx page, but was requested via the
/products/books.aspx url, then the browser will send a request for
/products/logo.jpg instead of /logo.jpg when it renders the page. To
reference this file correctly, make sure you root qualify CSS and Image
references ("/style.css" instead of "style.css"). For ASP.NET controls,
you can also use the ~ syntax to reference files from the root of the
application (for example: <asp:image imageurl="~/images/logo.jpg"
runat="server"/>
</quote>
Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 27 '07 #4

P: n/a
Hi Dan,

For the first question, I just tested it and I also found the issue you
mentioned. I will do some further research and get back to you.

For the second question, yes you can:

#How to Run a Root / Site with the VS/VWD 2005 Local Web Server -
ScottGu's Blog
http://weblogs.asp.net/scottgu/archi...21/431138.aspx

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 30 '07 #5

P: n/a
Hi Dan,

When you're using Context.RewritePath, you can specify an additional
parameter rebaseClientPath (False) for it and the "rootified" reference to
CSS or image should be working fine:

void Application_BeginRequest(object sender, EventArgs e)
{
string url = Request.Url.ToString();

if (url.Contains("/UrlRewrite/Products/Books.aspx"))
{

Context.RewritePath("~/UrlRewrite/Products.aspx?Category=Books", false);
}
}
Hope this helps.
Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 31 '07 #6

P: n/a
Hi Dan,

I'm writing to check the status of this post. Please feel free to let me
know if there's anything else I can help.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 3 '07 #7

P: n/a
Hi Walter,

Using false when doing Rewrite Path doesn't seem to make much difference to
me - I was using it that way anyway.

No problem though, the solution for using root path (/) when running a
website worked great, and gives me a better understanding of how its gonna
work once the site is on the internet.

Thanks,
Dan

""Walter Wang [MSFT]"" wrote:
Hi Dan,

I'm writing to check the status of this post. Please feel free to let me
know if there's anything else I can help.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 3 '07 #8

P: n/a
Hi Dan,

In my test, if I don't specify False for the rebaseClientPath parameter,
even when I used "~/..." in the CSS or image URL, they doesn't work when
the URL is rewritten. However, if I specified False for it, then they will
work correctly if I used "~/..." in the URL (please note for the "~/..." to
work correctly, the tag must have 'runat="server"'). Of course using a
static absolute URL "/..." will always work whether or not if the tag is
server-side or not.

Anyway, I'm glad the issue is now solved now. Please feel free to re-open
this post later if you find anything else unclear.
Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 6 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.