Using a colon (:) in a url with ASP.NET/IIS

前端 未结 8 2282
醉话见心
醉话见心 2020-12-05 19:14

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 valu

相关标签:
8条回答
  • 2020-12-05 19:31

    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.

    0 讨论(0)
  • 2020-12-05 19:45

    Is a colon valid in a url? Short answer no.

    Long answer, yes if it's in a url fragment.

    Example: http://site/gwturl#user:45/comments (note the colon proceeds the hash tag)

    Sources

    • this answer,
    • Is a colon safe for friendly-URL use?
    • along with a personal test of just adding : to a url in ASP.NET and getting the YSOD with A potentially dangerous Request.Path value was detected from the client (:)
    0 讨论(0)
  • 2020-12-05 19:47

    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.

    0 讨论(0)
  • 2020-12-05 19:47

    This web.config setting worked for me. It accepts colons (:) in the url.

    <httpRuntime targetFramework="4.6.1" requestPathInvalidCharacters=""/>
    
    0 讨论(0)
  • 2020-12-05 19:47

    Actually there is WCF REST available, and you can easily get up and running within an hour by using the WCF Starter Kit available here. This takes the power of REST and merges it with the ease of WCF. Also with WCF you can also create your own transport layer if you need to that can intepret URL's in any way you wish. One interesting thing about the starter kit is that it allowed spaces in the Url, which actually caused some headaches for true REST fundi's.

    I wasn't keen on looking at it due to WCF, but you really don't need to know that much. The solution creates everything you need, just add the code.

    0 讨论(0)
  • 2020-12-05 19:49

    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.

    0 讨论(0)
提交回复
热议问题