在我的 global.asax.cs 文件中。我添加一个条目
routes.MapRoute(
"Static text",
"Static/General/{filePath}",
new { controller = "Static", Action = "General", filePath = "" },
// new { filePath = @"xxxx" } // greedy regular expression
);
我想做的是从静态文件中获取内容并插入到我的 View 页面中。如果我的 filePath 位于根目录中(例如 1.txt),则此 map 工作正常。但如果文件位于某些子目录中,例如 staticfiles/1.txt,它将不起作用。因为路由模块会将 "staticfiles" 视为文件路径,并将 "1.txt" 保留为其他参数。我知道我需要做的是对 filePath 参数应用一些正则表达式技巧。但我无法弄清楚如何让正则表达式引擎一直读取到 url 的结尾。有人可以告诉我应该应用的技巧吗?非常感谢。
请您参考如下方法:
尝试在文件路径之前放置一个星号:
routes.MapRoute(
"Static text",
"Static/General/{*filePath}",
new { controller = "Static", Action = "General", filePath = "" }
);
