Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Unicode string displays incorrectly

I have the image:

http://localhost/WOZNIAK.png

This comes out fine. However, if I URL rewrite this with this rule:

<rewrite url="~/images/articles/(.*)$" to="~/handlers/displayimage.ashx?src=$1&amp;type=article" processing="stop"/>

And visit this URL:

http://localhost/images/articles/WOZNIAK.png

It doesn't show the image. My handler starts with the code:

public void ProcessRequest (HttpContext context) {

    string ImageSrc = context.Request.QueryString["src"];
    string ImageType = context.Request.QueryString["type"];

And if I take the ImageSrc value it outputs as:

WOŹNIAK.png

If I explicitly set ImageSrc to be the value ImageSrc = "WOZNIAK.png"; in the code the image display fine.

What do I need to do to allow these Unicode characters to pass properly? It's very problematic for us as we have users all over the world uploading large sets of images, some of which contain a lot of names with Unicode characters.

Renaming or modifying these images in any way is very problematic due to design, so I would really like a solution to this problem!

Edit

It appears I should probably Server.UrlEncode all query string params. Could this be the cause? I'm not sure if it's possible to do that in the URL rewrite rule though?

If I set the string to:

ImageSrc = context.Server.UrlDecode("WO%c5%b9NIAK.png");

It works. So I suppose the problem is URL encoding the querystring param in the rewrite rule.

like image 711
Tom Gullen Avatar asked Apr 09 '26 19:04

Tom Gullen


1 Answers

I fixed it. Intelligencia has the URL encode transform which can be used like in the following:

<rewrite url="~/images/articles/(.*)$" to="~/handlers/displayimage.ashx?src=${encode($1)}&amp;type=article" processing="stop"/>

Then display the image as follows in the code:

string ImageSrc = context.Server.UrlDecode(context.Request.QueryString["src"]);
like image 141
Tom Gullen Avatar answered Apr 12 '26 08:04

Tom Gullen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!