Html.ActionLink is not rendering # as expected

When using Html.ActionLink passing in a string containing # char it looks like it is, but if you UrlEncode it appears as% 2523.

I believe this is a mistake. Beta version of MVC.

Is this really a mistake?

http://example.com/test# appears as

http://example.com/test%2523 instead of

http://example.com/test%2523

+1


source to share


5 answers


Not a mistake. You don't want to UrlEncode actual urls. UrlEncode helps you encode text in URLs that may conflict with URI control characters. Likewise, you will not pass the actual HTML to the HtmlEncode unless you want to show your users the HTML itself.



+3


source


Yes, yes, run the following console application and see what it outputs:



using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string hash = "#";
            Console.WriteLine(HttpUtility.UrlEncode(hash));
        }
    }
}

      

+1


source


ok, found the problem ... I am using MVC and Html.ActionLink outputs # if I do not use UrlEncode, but if I do, it outputs% 2523 which is encoded in% 23 ....

maybe this is a mistake?

thank!

+1


source


Is HttpUtility.UrlEncode the same as Server.UrlEncode? (HttpContext.Current.Server.UrlEncode)

I can't do validation right now and not near the .NET machine.

0


source


Are you passing in # as part of the ActionLink parameters? If so, you should just add it like this: Html.ActionLink ("test") + "#"

0


source







All Articles