首页 > 解决方案 > 如何使用 MVC MapRoute 匹配与路径无关的文件扩展名?

问题描述

我正在尝试使用 MVC MapRoute 来重定向具有特定扩展名的任何文件,无论路径信息是什么 - 就像这样:

www.mywebsite.com/blah.xyz
www.mywebsite.com/whatever/hithere.xyz
www.mywebsite.com/test/morestuff/kittens.xyz
www.mywebsite.com/anything/everything/nothing/testing.xyz
www.mywebsite.com/1/2/3/4/5/6/7/8/9/alphabits.xyz

我创建了一个 routeBuilder 并且我拥有的 MapRoute 看起来像

routeBuilder.MapRoute("", "{filename}.xyz", new { controller = "Custom", action = "SpecialRedirect" });

我不确定如何将任何带有 .xyz 扩展名的页面/文件请求发送到处理方法,但是,我不确定如何忽略路径的其余部分。

有任何想法吗?

标签: c#asp.net-mvc.net-coreroutes

解决方案


看起来他们是没有办法做到这一点的,所以我使出了一堆这些。

   routeBuilder.MapRoute("", "{filename}.xyz",
        new { controller = "Custom", action = "SpecialRedirect" });
    
routeBuilder.MapRoute("", "{a}/{filename}.xyz",
    new { controller = "Custom", action = "SpecialRedirect" });

routeBuilder.MapRoute("", "{a}/{b}/{filename}.xyz",
    new { controller = "Custom", action = "SpecialRedirect" });

routeBuilder.MapRoute("", "{a}/{b}/{c}/{filename}.xyz",
    new { controller = "Custom", action = "SpecialRedirect" });

routeBuilder.MapRoute("", "{a}/{b}/{c}/{d}/{filename}.xyz",
    new { controller = "Custom", action = "SpecialRedirect" });

routeBuilder.MapRoute("", "{a}/{b}/{c}/{d}/{e}/{filename}.xyz",
    new { controller = "Custom", action = "SpecialRedirect" });

如果有人有更好的方法(这确实有效,但是对我来说很难看),请回答。


推荐阅读