不带尾随斜杠的URL导航?

不带尾随斜杠的URL导航?,url,path,nancy,Url,Path,Nancy,我们正在为我们的应用程序使用Nancy框架,该应用程序在控制台应用程序中自托管。 加载不带尾随斜杠的URL时出现问题 假设我们在中托管该页面 http://host.com:8081/module/ 然后它为我们提供am html页面,该页面具有如下相对路径的资源: content/scripts.js 当您输入一个url(如 // Generates a resource url 'http://host.com:8081/module/content/scripts.js' // wh

我们正在为我们的应用程序使用Nancy框架,该应用程序在控制台应用程序中自托管。 加载不带尾随斜杠的URL时出现问题

假设我们在中托管该页面

http://host.com:8081/module/
然后它为我们提供am html页面,该页面具有如下相对路径的资源:

content/scripts.js
当您输入一个url(如

// Generates a resource url 'http://host.com:8081/module/content/scripts.js' 
// which is good
http://host.com:8081/module/ 
但是,当我们省略一个尾随斜杠时,资源url是

// Generates a resource url 'http://host.com:8081/content/scripts.js' 
// which is bad
http://host.com:8081/module
有没有办法重定向到后面的斜杠版本?或者至少检测是否存在尾部斜杠


谢谢

这感觉有点老套,但它确实有效:

Get["/module/"] = o =>
{
    if (!Context.Request.Url.Path.EndsWith("/"))
        return Response.AsRedirect("/module/" + Context.Request.Url.Query, RedirectResponse.RedirectType.Permanent);
    return View["module"];
};
可从
上下文访问的
请求
允许您查看路径是否具有尾部斜杠,并重定向到“斜杠”版本。 我将其包装为一个扩展方法(适用于我非常简单的用例):

要在模块中使用:

// returns view "module" to client at "/module/" location
// for either "/module/" or "/module" requests
this.NewGetRouteForceTrailingSlash("module");

您可能会挂接到Before应用程序管道中,并添加尾部斜杠,或者执行直接调用Before不起作用,因为无论是否使用尾部斜杠访问URL,我都会得到相同的URL。我无法检测尾随斜杠何时丢失以及何时执行重定向。如果我创建了两条路由,
Get[“/module”]
Get[“/module/”]
,那么请求/module和/module/Get都由
Get[“/module/”]
路由处理,因此似乎无法区分它们?
// returns view "module" to client at "/module/" location
// for either "/module/" or "/module" requests
this.NewGetRouteForceTrailingSlash("module");