Tip/Trick: Url Rewriting with ASP.NET
page 5 of 6
by Scott Guthrie
Feedback
Average Rating: 
Views (Total / Last 10 Days): 93335/ 96

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"/>

Hope this helps,

Scott

P.S. For more ASP.NET 2.0 Tips and Tricks, please check out my ASP.NET 2.0 Tips, Tricks and Tutorials Page.

P.P.S. Special thanks to Scott Hanselman and Michael K. Campbell for testing my Form Control Adapter with their sites.


View Entire Article

User Comments

Title: Url Rewriting made Easy   
Name: ashish
Date: 2010-09-06 3:37:08 PM
Comment:
Very useful article. I liked it. Thanks a lot.
Title: Url Rewriting with ASP.NET   
Name: http://apps.facebook.com/socialcontest
Date: 2009-08-21 5:18:20 AM
Comment:
Hi,


I am using url rewriting and Ajax, now wherever anything related to ajax is done and the page is posted back, the postback url changes(i.e. its adding the directory name in the url) coz of which iam getting the following javascript error and i can't move forward.



Error: Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 500


I know it is an issue which lot of people are facing but how can i make url rewriting work with ajax..

The code which i have been using in Global is as follows:

string url = HttpContext.Current.Request.Url.AbsoluteUri;

if (url.Contains("404.aspx"))
{
string urlInfo405 = HttpContext.Current.Request.Url.AbsoluteUri;

string[] urlInfo404 = urlInfo405.Split(';');
string urlMain = urlInfo404[1];



string urlpath = urlMain.Replace("//", "/");

string[] urlInfo = urlpath.Split('?');
string path = urlInfo[0];
string[] currentPath = path.Split('/');


Context.Items.Add("Original_Path", currentPath[2] + ".aspx?cname=" + currentPath[3] + "&" + urlInfo[1]);
Context.RewritePath(currentPath[2] + ".aspx?cname=" + currentPath[3] + "&" + urlInfo[1],false);


I have even used Context.Items.Add which will fix my url for the entire post-response cycle.

Any guidance
Title: Nice article,   
Name: Baljeet Bhardwaj
Date: 2009-08-06 1:33:47 AM
Comment:
Hi..
nice article
Title: Found another working example   
Name: Samira
Date: 2009-04-07 2:32:22 AM
Comment:
Hi..
nice article but has few problems..i found another working article with example
see
http://dotnetkeeda.blogspot.com/2009/04/url-rewriting-mapping-in-aspnet.html
Title: nike shoes   
Name: nike shoes
Date: 2009-04-06 11:37:24 PM
Comment:
very good...
Title: Nice article, shame you copied it   
Name: Don't Copy Other People's Work
Date: 2009-01-25 4:39:02 PM
Comment:
Nice article, shame you copied it
Title: not working   
Name: milap
Date: 2008-05-12 6:46:39 AM
Comment:
hi please any example available for url rewriting then put here with code.
Title: not working   
Name: milap
Date: 2008-05-12 6:44:31 AM
Comment:
Hi in this website download link is not working so please check this.
Title: Not running on IIS localhost   
Name: rajiv Sharma
Date: 2008-03-15 2:03:09 AM
Comment:
Hi I am trying to use it, its working fien with ASP.NET Development Server, but it is not running on localhost (IIS) server, please help me. rajrunatserver@gmail.com
Title: QueryString   
Name: Hugo
Date: 2008-02-20 3:27:59 PM
Comment:
Very good your article!!! Fantastic!

One more question... How can I do for this url: http://localhost:49195/UrlRewrite_HttpModule1/Products/Books.aspx?varB=1&varB=xxx

I want read querystring too...

Thanks. You can reply to me for hgoncalves@gmail.com
Title: Great Article   
Name: Adam Lock
Date: 2008-02-12 6:29:31 AM
Comment:
Very information and well written - thanks
Title: in a shared hosting environment   
Name: Carlos Martinez
Date: 2007-12-04 9:55:29 AM
Comment:
Hi, You said the first approach could work in a shared hosting environment. however, we always have to configure the IIS server when deployment. Or there is another option?? I have this problem now, and I have asked my hosting to allow me that.
Title: please help me config on server   
Name: bnet
Date: 2007-05-19 12:43:56 AM
Comment:
please help me config on server
Title: can not run on server   
Name: bang
Date: 2007-05-18 9:40:10 PM
Comment:
can not run on server
please help me
thank you

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-19 4:07:40 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search