I'm implementing a custom controller in ASP.NET MVC and really want to be able to use a colon in the urls, so that I can identify class/column names and their values, like so:
http://example.com/user:chaiguy
...but apparently ASP.NET or IIS doesn't allow colons in urls. I did some digging and apparently it's considered a security issue, but, I'm using MVC and am handling all url paths manually (just treating them as strings), and not relating them to the file system, so I'm pretty sure this doesn't apply.
I also heard some talk about implementing a custom Http handler or something.
Any thoughts or ideas would be much appreciated.
Er.... why? Seriously, why break standards? – Randolpho
...
I suggest, then, that you investigate building a web service. WCF is a nice technology for that, and it hosts well in IIS.
I happen to like urls, and WCF is way too complicated for my purposes. I want it to be url-compatible, like REST, but capable of more than just navigating hierarchies, or doing well laid-out things. The problem I have with /users/chaiguy is that it is interpreting hierarchy where there is none: in my system "user" is a class, it's not a folder. user:chaiguy means the instance of the user class with the value of "chaiguy", and that is a single entity, that has the potential of having child-entities. So for example:
/user:chaiguy/name
...I would like to display the name of that entity. If I did this with your method, it would look like this:
/users/chaiguy/name
The problem is how do you know what's the class and what's the value? It could be interpreted as
/users/chaiguy:name
in my system, and that doesn't make sense. See what I'm getting at? To give a slightly more complicated example, suppose we want to select a child of the user entity out of multiple instances. So a user might have several email addresses. To select one, we might use:
/user:chaiguy/email:[email protected]/
So it is in fact recursive. It's not a file path, it's more like an XPath (or maybe similar to jQuery based on what little I know of it yet). That is, it's more of a dynamically-evaluated query selection than a hardwired file path. It gets evaluated on the server.
Make no mistake, I'm not building a typical web site or even web service here.
It is completely fine to use a colon : in a URL path.
It's just a separator. It doesn't 'mean' or 'specify' anything. In your own example it is also used to separate the scheme from the hostname.
Change the requestPathInvalidCharacters
attribute of httpRuntime
in web.config:
<httpRuntime maxRequestLength="20480" requestValidationMode="2.0" requestPathInvalidCharacters="" maxQueryStringLength="20480" />
and ASP.NET should no longer block colons from your request path.
Answered similar question here: https://stackoverflow.com/a/12037000/134761
It seems that ASP.net does not allow colons before the '?' in an URL, even if it is encoded as %3A.
For example, these won't work:
http://foo.org/api/persons/foo:bar
http://foo.org/api/persons/foo%3abar
But this works:
http://foo.org/api/persons?id=foo%3abar
In all examples, we would expect ASP.NET MVC to pass "foo:bar" as an id argument, properly decoded. I just tested this with MVC4 and it seems to work. It is annoying that it doesn't accept the URL encoding before the question mark though, but I'm sure there is a good reason for it. Probably to keep everything before the question mark a valid URL and any arguments after the question mark.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With