I was reading articles on URL Rewriting in ASP.NET and I found the most common way suggested is to use Context.RewritePath either in global.asax file or in HttpHandler with Application_BeginRequest method. The most common found code is Context.RewritePath(NewURL). When you the application you find it works like a charm and yes it does so!!!!.
However sometime Images and CSS are not displayed on page when we use Context.RewritePath. Let me be more specific if you have a hierarchy as shown below,
Fig - (1) Website structure
Now if you have used images on Default2.aspx and also have css for controls. We have redirected user to Default2.aspx page using Context.RewritePath path when there is no specific file available physically. So here if we write the URL http://localhost:3030/Example1/Test.aspx then we are redirected to Default.aspx page and images will be displayed properly. However id you type URL http://localhost:3030/Example1/Products/Test.aspx then we are redirected to Default.aspx page however images will not be displayed.
Even if you have assign ImageURL using “~/Images/Image1.jpg” you find that images will not be displayed. The image path will always taken as relative path with the URL mentioned in address bar. So in first case the virtual path of image becomes Example1/Images/Image1.jpg while in second case the virtual path of image becomes Example1/Products/Images/Image1.jpg. So in second case images and css will not displayed with Context.RewritePath.
To solve this use second argument with Context.RewritePath so the function will be Context.RewritePath(NewURL,false) and everything will work fine. if second argument is true reset the virtual path else the virtual path will not be changed. As we want virtual path to be unchanged we have to pass false in second argument.
Pleas read this article for more detail.
Happy Programming !!!!
No comments:
Post a Comment