473,320 Members | 1,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

URL Rewriting and file paths

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
8 4264
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Dave Smithz | last post by:
Hi There, I have taken over someone else's PHP code and am quite new to PHP. I made some changes and have implemented them to a live environment fine so far. However, I now want to setup a...
1
by: jnc | last post by:
Hi, I was wondering if anybody had any pointers on the following: I need to write some code which takes a list of file paths and transforms them into a treeview which shows the directory...
7
by: Travis Pupkin | last post by:
Hi, Just curious: is there a bit of ASP code you can place in the top of a document that will force all root relative image and file paths in the HTML of this doc to expand to include the full...
11
by: BoonHead, The Lost Philosopher | last post by:
I think the .NET framework is great! It's nice, clean and logical; in contradiction to the old Microsoft. It only saddens me that the new Microsoft still doesn't under stand there own...
3
by: William Starr Moake | last post by:
Is there a script to convert image file paths from absolute to just img src="imagename.jpg"? This is for an IE-based WYSIWYG editor. The doImage execCommand produces an absolute file path, which...
1
by: Pete | last post by:
I know MSBUILD doesn't like spaces in file paths when used as values of elements but I cannot find anything which documents the recommended handling etc... Any and all suggestions are...
0
by: Steve | last post by:
Hi, I'm developing a web app on a Windows PC and deploying on Mac OS X Server. (Both Java 1.4.2, Tomcat 5.5.17). What's the best way to transparently different file paths on the different...
17
by: DesCF | last post by:
The following doesn't work: Dim photoPath As String = "R:\My Documents\My Pictures\MiscLL\" It doesn't work if I use double quotes either. It does work if I use a different path without...
0
by: CGambino | last post by:
Hello, I was attempting to implement simple URL Rewriting using an HTTP Module with an event on BeginRequest and powered by a Context.RewritePath. I got everything to work fine. When I was...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.