如何在URL(较大的ASP.NET应用程序中的Web Api)中正确支持冒号(\\’:\\’)

How do I properly support colons (':') in URLs (WebApi within larger ASP.NET application)

我希望我的一些ID用冒号分隔,这意味着我的URL路径中包含冒号,例如/somethings/xyz:123

我已经从requestPathInvalidCharacters中删除了:,但是现在我遇到了另一个问题。外部应用程序中似乎有几个第三方模块可以对所有请求获取PhysicalPath或执行MapPath,这似乎与冒号有关:

1
2
3
4
5
6
7
[NotSupportedException: The given path's format is not supported.]
   System.Security.Util.StringExpressionSet.CanonicalizePath(String path, Boolean needFullPath) +14633709
   System.Security.Util.StringExpressionSet.CreateListFromExpressions(String[] str, Boolean needFullPath) +351
   System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, AccessControlActions control, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList) +151
   System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList) +38
   System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess access, String path) +92
   System.Web.Hosting.HostingEnvironment.MapPath(VirtualPath virtualPath) +107

如何解决它而不重写所有这些模块?


我找到了解决PhysicalPathMapPath(对于所有模块)中的异常的答案:

<httpRuntime ... relaxedUrlToFileSystemMapping="true" />


我自己还没有测试过,但这可能会有所帮助。检查是否安装了此修补程序-http://support2.microsoft.com/kb/932552

然后根据文档,您必须更改以下注册表设置。

应用此修补程序后,必须通过将以下注册表子项的VerificationCompatibility项的值设置为1来启用它:

1
2
3
4
HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\ASP.NET

DWord Value Name: VerificationCompatibility
Value Data: 1