19

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:me@here.com/

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.

4

8 回答 8

20

更改web.config中的requestPathInvalidCharacters属性:httpRuntime

<httpRuntime maxRequestLength="20480" requestValidationMode="2.0" requestPathInvalidCharacters="" maxQueryStringLength="20480" />

并且 ASP.NET 不应再阻止您的请求路径中的冒号。

于 2011-04-25T21:40:25.480 回答
4

在这里回答了类似的问题:https ://stackoverflow.com/a/12037000/134761

似乎 ASP.net 不允许在“?”之前使用冒号。在 URL 中,即使它被编码为 %3A。

例如,这些将不起作用:

http://foo.org/api/persons/foo:bar

http://foo.org/api/persons/foo%3abar

但这有效:

http://foo.org/api/persons?id=foo%3abar

在所有示例中,我们希望 ASP.NET MVC 将“foo:bar”作为 id 参数传递,并正确解码。我刚刚用 MVC4 测试了这个,它似乎工作。令人讨厌的是它不接受问号之前的 URL 编码,但我确信这是有充分理由的。可能将问号之前的所有内容保留为有效的 URL 以及问号之后的任何参数。

于 2012-08-23T05:20:57.580 回答
1

I suggest you rethink what you want to do. Use pathing to indicate context and hide your class and field names, mapping particular contexts within your URL paths to class names and fields. If you need to indicate a user, for example, build your URL layout like example.com/users/chaiguy rather than example.com/user:chaiguy.

于 2009-03-20T18:50:44.673 回答
1

尝试设置 HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\HTTP\Parameters\AllowRestrictedChars。这是来自http://support.microsoft.com/?id=820129。我不知道 ASP.NET/MVC 是否自己做一些检查,但如果它只是 http.sys 阻止你,这应该可以解决它。

于 2009-07-07T09:06:12.970 回答
1

这个 web.config 设置对我有用。它在 url 中接受冒号 (:)。

<httpRuntime targetFramework="4.6.1" requestPathInvalidCharacters=""/>
于 2016-12-15T15:24:54.387 回答
0

实际上有 WCF REST 可用,您可以使用此处提供的 WCF 入门工具包在一小时内轻松启动并运行。这利用了 REST 的强大功能并将其与 WCF 的易用性相结合。此外,使用 WCF,您还可以创建自己的传输层,如果需要,可以以任何您希望的方式解释 URL。关于初学者工具包的一件有趣的事情是它允许在 Url 中使用空格,这实际上给真正的 REST Fundi 带来了一些麻烦。

由于 WCF,我并不热衷于查看它,但您真的不需要知道那么多。该解决方案创建您需要的一切,只需添加代码。

于 2009-07-07T09:14:34.310 回答
-4

我建议使用句号。基于 HTTP 协议的 REST 是一个为 HTTP 构建新用途的示例,该用途符合标准并且非常成功。也许你可以这样做。

和一个“。” 在许多语言中是标准的“class.method”或“class.attribute”。

现在ME,我想在URL参数中使用冒号,有些地方正在这样做。我还得看看我能不能摆脱它。

PS,对我来说,我可以使用这个: http ://www.businesscasualblog.com/2009/07/how-to-share-a-link-to-a-specific-timecode-in-youtube-video.html

本质上是'--h--m--s'

于 2012-01-02T03:13:18.497 回答
-10

冒号在 url 中有效吗?简短的回答没有

长答案,是的,如果它在一个 url片段中。

示例:(http://site/gwturl#user:45/comments注意冒号是哈希标签)

来源

于 2011-04-04T15:08:35.863 回答