如何通过文件扩展名设置匹配任何路径的路由(How to set up a route that matches any path by file extension)

编程入门 行业动态 更新时间:2024-10-23 05:35:44
如何通过文件扩展名设置匹配任何路径的路由(How to set up a route that matches any path by file extension)

我希望NancyFX中的路由匹配jpg路径,而不管路径段的数量。 例如,它应该匹配所有:

/image.jpg /foo/image.jpg /foo/bar/baz/foo/baz.jpg

这可能吗? 似乎我为NancyFX找到的所有通配符选项都是针对特定路径段而不允许多个段(其中段是由'/'分隔的部分)。

看起来它应该是可能的,因为静态内容服务器需要能够做到这一点。

I want to have a route in NancyFX match an jpg path, regardless of the number of path segments. For example, it should match all of:

/image.jpg /foo/image.jpg /foo/bar/baz/foo/baz.jpg

Is this possible? It seems all the wildcard options I find for NancyFX are for a particular path segment and do not allow multiple segments (where a segment is a portion separated by '/').

It seems like it should be possible, as the static content server would need to be able to do this.

最满意答案

Nancy支持路由中的正则表达式。 您可以使用正则表达式将所有路径与.jpg图像匹配,并捕获图像的路径和名称:

public MyModule : NancyModule { public MyModule() { Get[@"(?<imagepath>.*)/(?<imagename>.*.jpg)"] = params => { string path = params.imagepath; string name = params.imagename; return DoStuff(path, name); } } }

Nancy supports regular expressions in routes. You can use a regular expression to match all the routes to .jpg images, and capture the path and name of the images:

public MyModule : NancyModule { public MyModule() { Get[@"(?<imagepath>.*)/(?<imagename>.*.jpg)"] = params => { string path = params.imagepath; string name = params.imagename; return DoStuff(path, name); } } }

更多推荐

本文发布于:2023-08-07 12:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464250.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   路径   文件扩展名   set   route

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!