如何在 C# MVC 路由表中屏蔽波形符 (~) 字符?
我正在将我的自制网站迁移到 MVC,但在 url 路由方面遇到了麻烦。该站点已提供多个路径中包含波形符 (~) 字符的链接;类似的东西
http://<root>.../~files/...
http://<root>.../~ws/...
,我希望它们中的每一个都由单独的控制器处理,例如 filesController、wsController,所以我的路由表看起来像
routes.MapRoute( "files", "~files/{*prms}", new { controller = "files", action = "index", prms = "" } );
routes.MapRoute( "ws", "~ws/{*prms}", new { controller = "ws", action = "index", prms = "" } );
......
但是当我尝试获取结果时,我收到错误消息“路由 URL 不能以 ' 开头/' 或 '~' 字符且不能包含 '?'特点。”
据我了解,这些字符在 ASP.net 中具有特殊含义,但是是否可以以某种方式屏蔽它们,至少是波形符?我应该自己解析和路由这样的请求吗?处理这样的 url 的最佳做法是什么?
I'm moving my home-baked web site to MVC and got the trouble with url routing. The site already serves several links that contain tilde (~) character in the path; something like
http://<root>.../~files/...
http://<root>.../~ws/...
and I want each of them are handled by separate controller, like filesController, wsController, so my route table looks like
routes.MapRoute( "files", "~files/{*prms}", new { controller = "files", action = "index", prms = "" } );
routes.MapRoute( "ws", "~ws/{*prms}", new { controller = "ws", action = "index", prms = "" } );
...
but when I try to get the result I got the error saying "The route URL cannot start with a '/' or '~' character and it cannot contain a '?' character."
As I understand those characters have the special meaning in ASP.net but is it possible to mask them somehow, at least tilde? Should I parse and route requests like this myself? What the best practice to handle urls like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我看不出你能做到这一点。在 ASP.NET 中,波浪号字符代表应用程序的根,内部路由将使用它来重新设置其 URI 的基础。您也许可以在更高级别上执行此操作(例如 IIS7 的 URL 路由模块)并在其 MVC 之前重新映射 url?
I can't see that you can do this. In ASP.NET the tilde character represents the root of the application, and routes internally will use it to rebase their URIs. You might be able to do it at a higher level (like the URL Routing module for IIS7) and remap the urls before it his MVC?