Click here to Skip to main content
16,015,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following code on my site:

C#
<a href='<%# VirtualPathUtility.ToAbsolute("~/Products.aspx?category="+Eval("category")) %>'> <img src='<%# "/Store/Images/Thumbs/"+ Eval("CategoryImage") %>' /></a>


This displays imags for various categories in a database. When a user clicks on the image, i expect a redirect to products page which displays products for the selected category.

It works fine on localhost on visual studio but when i uplaod it to server, I get the error that '~/Products.aspx?category=selectedcategory' is not a valid virtual path.

I have tried using '/Products.aspx' and 'Products.aspx' in place of '~/Products.aspx' but i get the same error.
Posted

1 solution

Try moving the query-string outside of the ToAbsolute call:
C#
VirtualPathUtility.ToAbsolute("~/Products.aspx") + Eval("category", "?category={0}")


The reason this code works on your local machine but not on the server is most likely that it's configured as a .NET 4.0 application locally, but a .NET 2.0 / 3.5 application on the server. As mentioned in the Community Comments on the version-specific MSDN page[^], the .NET 2.0 version of this method incorrectly treated ? as an invalid character.
 
Share this answer
 
Comments
Member 10235977 6-Aug-14 6:11am    
Thanks but it did not work for me. I still get the same error: '~/Products.aspx?category=selectedcategory' is not a valid virtual path.
Richard Deeming 6-Aug-14 7:23am    
With what code? If you're getting that error from the VirtualPathUtility.ToAbsolute method, then you're not using the code I suggested; you're still passing the query-string into that method.
Member 10235977 7-Aug-14 5:54am    
Yes you are right. It works. Many thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900