4

I recently detected, that UrlHelper.IsLocalUrl method always returns false, if it detects non-ASCII characters in the url parameter.

Example:

var isLocal = UrlHelper.IsLocalUrl("контакты"); //false

Is it a bug, or "by design"?

s.ermakovich
  • 2,641
  • 2
  • 26
  • 24

2 Answers2

17

The latest available implementation is checking for those conditions:

  • the url starts with "/" and is not followed by "/" or "\"
  • or the url starts with "~" and is followed by "/"

Thus all the urls passed to this method must start with "/" or "~/".

Also, in case you wonder, it doesn't take the current host into account and check for schemes like http for instance.

Update: Here is a link to the implementation which is used by UrlHelper.IsLocalUrl http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/600963a4df15#src/System.Web.WebPages/RequestExtensions.cs

jv-dev
  • 646
  • 7
  • 14
Sébastien Ros - MSFT
  • 5,091
  • 29
  • 31
  • Hi Sebastian! It seems that you are talking about Orchard CMS, when this question is related to ASP.NET MVC in general :) I saw that you've added a custom implementation of the `IsLocalUrl` in Orchard recently, but I didn't have a time to test it yet. Will try to provide a feedback asap. Thanks! – s.ermakovich Oct 06 '12 at 09:34
  • I am not talking about Orchard, but MVC 4 itself. Because of the MVC behavior we did implement a custom one as you noticed. – Sébastien Ros - MSFT Oct 10 '12 at 19:09
  • I'm accepting this answer, because it better reflects current state. – s.ermakovich Oct 11 '12 at 07:54
  • 2
    For those who are interested: The behaviour has changed since MVC 3. In MVC 3 the current host was taken into place and now it is not anymore, unfortunately. This was a breaking change. – Peit Mar 25 '13 at 09:32
  • The link is not working, is this a problem with CodePlex? – jv-dev Aug 30 '16 at 14:50
1

Maybe because the URL in RFC is defined by the US-ASCII code set, with reserved characters.
You can read about URL specification here: http://www.ietf.org/rfc/rfc1738.txt

And a quote from the above site/document:

URLs are written only with the graphic printable characters of the US-ASCII coded character set. The octets 80-FF hexadecimal are not used in US-ASCII, and the octets 00-1F and 7F hexadecimal represent control characters; these must be encoded.

So in short, my guess is "by design".

Note: RFC is set by the IETF, which sets the standards of some technologies such as URL.

Johnny
  • 785
  • 1
  • 7
  • 16
  • 1
    Yes, it seems that ASP.NET MVC`s `UrlHelper.IsLocalUrl` implementation strictly follows RFC 1738. To be honest, I don't understand why they do strictly follow almost 20-years old standard. The internet evolves, and it needs to be taken into consideration, if ASP.NET MVC still wants to be called "a modern web framework". – s.ermakovich Oct 06 '12 at 10:20