ASP.NET 4 路由包罗万象
我有一个使用路由的 ASP.NET 4 WebForms 应用程序。我想捕获不存在的路由的 404:
RouteTable.Routes.MapPageRoute("404", "{*url}", "~/error");
问题是,这也会导致 ImageHandler.ashx
和 等页面映射到
。/error
资源.axd
所以我添加了这个:
RouteTable.Routes.Ignore("{resource}.axd");
RouteTable.Routes.Ignore("{handler}.ashx");
但这只会忽略根目录中的Resource.axd,而不是例如/scripts/Resource.axd
中的Resource.axd。
我怎样才能做到这一点?或者我应该为捕获所有 PageRoute 设置什么约束,以便它只捕获目录?
I have an ASP.NET 4 WebForms application which is using routing. I would like to catch the 404's for routes that do not exist:
RouteTable.Routes.MapPageRoute("404", "{*url}", "~/error");
Problem is, this will also cause a mapping to /error
for pages like ImageHandler.ashx
and Resource.axd
.
So I add this:
RouteTable.Routes.Ignore("{resource}.axd");
RouteTable.Routes.Ignore("{handler}.ashx");
But this only ignores Resource.axd in the root directory, not in for example /scripts/Resource.axd
.
How can I accomplish this? Or what Contraints should I set for the catch all PageRoute so it will only catch directories?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在混合 WebForms/MCV3 应用程序中发现了类似的问题,这让我想到了这个问题。基本上只有根级 axd 文件得到了正确处理,除此之外的任何内容都试图路由到控制器。
我的情况可能有所不同,但最终我发现这样做是有效的:
我对 Ignore 和 IgnoreRoutes 不太了解,因此可能有更好/不那么贪婪的解决方案,但它正在发挥作用。
I found a similar problem with a hybrid WebForms/MCV3 application which brought me to this question. Basically only root level axd files were handled properly, anything outside of that was trying to be routed to a controller.
My situation maybe different, but in the end I found doing something like this worked:
I don't know much about Ignore and IgnoreRoutes so there could be a better/less greedy solution, but it's working.
你尝试过这个吗?
Did you try this?