.NET Forum / ASP.NET / General / August 2007
URL Rewriting and file paths
|
|
Thread rating:  |
musosdev - 26 Jul 2007 22:46 GMT 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
nirmal.c@gmail.com - 27 Jul 2007 07:21 GMT 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
> Hi guys > [quoted text clipped - 23 lines] > > Dan Walter Wang [MSFT] - 27 Jul 2007 08:55 GMT 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/archive/2007/02/26/tip-trick-url-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 (wawang@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.
musosdev - 27 Jul 2007 13:52 GMT 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
> Hi Dan, > [quoted text clipped - 36 lines] > > This posting is provided "AS IS" with no warranties, and confers no rights. Walter Wang [MSFT] - 30 Jul 2007 10:53 GMT 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/archive/2005/11/21/431138.aspx
Regards, Walter Wang (wawang@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.
Walter Wang [MSFT] - 31 Jul 2007 06:45 GMT 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 (wawang@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.
Walter Wang [MSFT] - 03 Aug 2007 04:37 GMT 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 (wawang@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.
musosdev - 03 Aug 2007 10:52 GMT 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
> Hi Dan, > [quoted text clipped - 11 lines] > > This posting is provided "AS IS" with no warranties, and confers no rights. Walter Wang [MSFT] - 06 Aug 2007 02:16 GMT 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 (wawang@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.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|