c# - 如何使用 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 扩展名的页面/文件请求发送到处理方法,但是,我不确定如何忽略路径的其余部分。
有任何想法吗?
解决方案
看起来他们是没有办法做到这一点的,所以我使出了一堆这些。
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" });
如果有人有更好的方法(这确实有效,但是对我来说很难看),请回答。
推荐阅读
- python - Python:与非 Python 用户共享交互式绘图(离线)
- java - 我如何在 listview android 中使用我的 JsonArray?
- powerbi - DAX Power BI - 如何计算表中出现超过 5 次的 ID
- r - 如何使用 ifelse 对一系列变量进行分组
- php - 存储过程中的多个查询在 codeigniter 中不起作用
- sql - 在 SQL 中查找日期的最后 3 个不同的事物顺序
- google-sheets - 谷歌表格 - 交叉数据的功能 - 数据范围问题
- c# - 如何将 Mediatr 命令错误返回给控制器?
- python - 如何从烧瓶restful API返回图像?
- angular - 如何在特定索引处显示项目,然后在Angular 2+中的每五个元素之后显示