Nancy URL navigation without a slash?

We use the Nancy framework for our application, which is standalone in the console application. The problem occurs when loading a URL without a slash.

Say we host a page in

http://host.com:8081/module/

Then it serves us as an html page that has resources with a relative path as follows:

content/scripts.js

Everything works well when you enter a url like

// Generates a resource url 'http://host.com:8081/module/content/scripts.js' 
// which is good
http://host.com:8081/module/ 

But when we leave the trailing slash, the URL of the resource

// Generates a resource url 'http://host.com:8081/content/scripts.js' 
// which is bad
http://host.com:8081/module

Is there a way to make the redirect to the trailing slash? Or at least determine if the trailing slash exists or not.

Thank!

+4
source share
1 answer

This is a little annoying, but it works:

Get["/module/"] = o =>
{
    if (!Context.Request.Url.Path.EndsWith("/"))
        return Response.AsRedirect("/module/" + Context.Request.Url.Query, RedirectResponse.RedirectType.Permanent);
    return View["module"];
};

Request, Context, , "" . ( ):

public static class NancyModuleExtensions
{
    public static void NewGetRouteForceTrailingSlash(this NancyModule module, string routeName)
    {
        var routeUrl = string.Concat("/", routeName, "/");
        module.Get[routeUrl] = o =>
        {
            if (!module.Context.Request.Url.Path.EndsWith("/"))
                return module.Response.AsRedirect(routeUrl + module.Request.Url.Query, RedirectResponse.RedirectType.Permanent);
            return module.View[routeName];
        };
    }
}

:

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

, ,

0

All Articles