是否有可能匹配ASP.NET MVC 6路由映射中任何以前不匹配的URL?

时间:2016-04-14 17:30:04

标签: asp.net-mvc routing

我想自定义不匹配的404页面,我指定了"模板"参数为" {*}"在路由器" all"中,旨在捕获与"默认"不匹配的所有网址(例如" http://localhost:12345/aaa/bbb/ccc/ddd/")路由器。有没有办法这样做?

app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{Controller:regex((?i)^((?!Error).)*$)}/{Action}/{id?}",
                defaults: new { Controller = "Home", Action = "Index" })

            //TODO: This Route is not working as expected.
            .MapRoute(
                name: "all",
                template: "{*}", //<---------
                defaults: new { Controllers = "Error", Action = "Unknown" });
        });

1 个答案:

答案 0 :(得分:0)

你可以这样试试&#34; {* url}&#34;或&#34; / {* url}&#34;。我认为它需要一些通配符之后才能使用捕获的url作为参数。