1

In my ASP.NET WebForm application I have simple rule:

routes.MapPageRoute("RouteSearchSimple", "search/{SearchText}", "~/SearchTicket.aspx");

As "SearchText" param I need to use cyrillic words, so to create Url I use:

string searchText = "текст";
string url = Page.GetRouteUrl("RouteSearchSimple",
            new
            {
                SearchText = searchText
            });

GetRouteUrl automatically encode searchText value and as a result url = /search/%D1%82%D0%B5%D0%BA%D1%81%D1%82

but I need -> /search/текст

How is it possible to get it by Page.GetRouteUrl function.

Thanks a lot!

ihorko
  • 6,855
  • 25
  • 77
  • 116
  • 1
    Is your question "how to create invalid Url"? The path of Uri can't contain non-ASCII characters - http://www.ietf.org/rfc/rfc3986.txt – Alexei Levenkov Jun 06 '13 at 20:25
  • 1
    Actually it can in all browsers, you pointed to some 8 years old document – ihorko Jun 06 '13 at 20:35
  • 1
    Would you mind providing link to a new document? You may be talking about "browser display unencoded Urls in address", but I'd be glad to be wrong and see document that specify how non-encoded non-ASCII text should be specified in path portion. – Alexei Levenkov Jun 06 '13 at 21:37

3 Answers3

2

Actually, I believe Alexei Levenkov is close to the answer. Ultimately, a URL may only contain ASCII characters, so anything beyond alphanumeric characters will be URL encoded (even things like spaces).

Now, to your point, there are browsers out there that will display non-ASCII characters, but that is up to the implementation of the browser (behind the scenes, it is still performing the encoding). GetRouteUrl, however, will return the ASCII-encoded form every time because that is a requirement for URLs.

(As an aside, that "some 8 year old document" defines URLs. It's written by Tim Berners Lee. He had a bit of an impact on the Internet.)

Update

And because you got me interested, I did a bit more research. It looks as though Internationalized Domain Names do exist. However, from what I understand from the article, underneath the covers, ToASCII or ToUnicode are applied to the names. More can be read in this spec: RFC 3490. So, again, you're still at the same point. More discussion can be found at this Stackoverflow question.

Community
  • 1
  • 1
JasCav
  • 34,458
  • 20
  • 113
  • 170
0

Ok, guys, thank you for replies, it helps much. Simple answer is: it's impossible to do that by Page.GetRouteUrl() function. It's very strange why it hasn't beed developed in way to rely Encoding/Decoding params on developers like we have it in Request.Params or .QueryString, or at least it would be some alternate routing function where developers could control that.

One way I found is getting Url from RouteTable and parse it manually, in my case it would be like:

string url = (System.Web.Routing.RouteTable.Routes["RouteSearchSimple"] as System.Web.Routing.Route).Url.Replace("{SearchText}", "текст");

or simplest way is just creating url via string concatenation:

string param = "текст";
string url = "/search/" + param;

what I already did, but in that case you will need change the code in all places where it appears if you change your route url, therefore better create some static function like GetSearchUrl(string searchText) in one place.

And it works like a charm, Url's looks human readable and I can read params via RouteData.Values

ihorko
  • 6,855
  • 25
  • 77
  • 116
0

The most simple solution is to decode with UrlDecode method:

  string searchText = "текст";
  string url = Page.GetRouteUrl("RouteSearchSimple",
            new
            {
                SearchText = searchText
            });
  string decodedUrl = Server.UrlDecode(url); // => /search/текст
hawk
  • 5,409
  • 2
  • 23
  • 29