如何将引号字符包含为路由参数?获取“路径中的非法字符”消息

时间:2010-10-19 12:17:40

标签: asp.net-mvc encoding routing

我在所有网页上都有一个搜索框。用户可以输入文本,然后单击搜索,它们最终会显示在“搜索”页面上,并显示结果。

单击搜索时,包含文本框的表单将发送到以下路径:

http://localhost:2010/search/my%20search%20term
这是有效的。但如果我在字符串周围加上引号,

http://localhost:2010/search/%22my%20search%20term%22

此操作失败,并显示错误消息“路径中存在非法字符”。

使用以下堆栈跟踪:

[ArgumentException: Illegal characters in path.]
   System.IO.Path.CheckInvalidPathChars(String path) +7493413
   System.IO.Path.Combine(String path1, String path2) +40
   System.Web.Configuration.UserMapPath.GetPhysicalPathForPath(String path, VirtualDirectoryMapping mapping) +114
   System.Web.Configuration.UserMapPath.GetPathConfigFilename(String siteID, VirtualPath path, String& directory, String& baseName) +72
   System.Web.Configuration.UserMapPath.MapPath(String siteID, VirtualPath path) +30
   System.Web.Configuration.UserMapPath.MapPath(String siteID, String path) +31
   System.Web.Hosting.HostingEnvironment.MapPathActual(VirtualPath virtualPath, Boolean permitNull) +297
   System.Web.Hosting.HostingEnvironment.MapPathInternal(VirtualPath virtualPath, Boolean permitNull) +51
   System.Web.CachedPathData.GetConfigPathData(String configPath) +341
   System.Web.CachedPathData.GetVirtualPathData(VirtualPath virtualPath, Boolean permitPathsOutsideApp) +110
   System.Web.HttpContext.GetFilePathData() +36
   System.Web.HttpContext.GetConfigurationPathData() +26
   System.Web.Configuration.RuntimeConfig.GetConfig(HttpContext context) +43
   System.Web.Configuration.CustomErrorsSection.GetSettings(HttpContext context, Boolean canThrow) +41
   System.Web.HttpResponse.ReportRuntimeError(Exception e, Boolean canThrow, Boolean localExecute) +101
   System.Web.HttpRuntime.FinishRequest(HttpWorkerRequest wr, HttpContext context, Exception e) +383

我不确定这里有什么问题。我需要以某种方式对输入进行编码吗?我是否需要在路由中指定不同的内容?

1 个答案:

答案 0 :(得分:2)

这是来自Scott Hanselman的blog post,应该会给你一些好的指示。