Routes that overlap with folders

24 views
Skip to first unread message

hcoverlambda

unread,
Feb 5, 2012, 10:16:21 PM2/5/12
to FubuMVC Development Group
Just a note if anyone is interested. You can configure routing to
match routes that overlap with folders and files by setting the
following flag to true:

RouteTable.Routes.RouteExistingFiles

This enables a potential url policy where a handler url is the same as
a folder, like /users for example, and you could serve a static file
at /users/users.js. There are obviously downsides to this if you have
url parameters (i.e. /users/{username}) as the url would then map to
that route and not the file. But in cases where you're not using url
parameters (And doing client side routing in backbone or whatever)
this may have some utility.

ahjohannessen

unread,
Feb 7, 2012, 5:47:07 PM2/7/12
to fubumv...@googlegroups.com
Thanks for the tip :)

Mike O'Brien

unread,
Feb 12, 2012, 9:54:54 PM2/12/12
to fubumv...@googlegroups.com
Actually one thing I hit on this is that the id url parameter will override static files in the folder. So you really want to only override folders and not files. The only way I could figure out how to do that is to turn on that flag and then create a route that ignores static files. So static files will always trump routes but routes will always trump directories.

RouteTable.Routes. RouteExistingFiles = true;
RouteTable.Routes.Add(new IgnoreFilesRoute());

public class IgnoreFilesRoute : Route
{
    public IgnoreFilesRoute() : base(null, new StopRoutingHandler()) { }

    public override RouteData GetRouteData(HttpContextBase httpContext)
    {
        return HostingEnvironment.VirtualPathProvider.FileExists(httpContext.Request.AppRelativeCurrentExecutionFilePath) ? 
            new RouteData(this, RouteHandler) : null;
    }

    public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary routeValues)
    {
        return null;
    }
}

This will allow for a url convention along these lines:

/users/GetAllHandler.cs --> /users:GET
/users/GetHandler.cs --> /users/{id}:GET
/users/PostHandler.cs --> /users:POST
/users/PutHandler.cs --> /users/{id}:PUT
/users/DeleteHandler.cs --> /users/{id}:DELETE
/users/app.js --> /users/app.js

Anyways, not sure if this is a good idea or not yet but it is possible.

On Tue, Feb 7, 2012 at 5:47 PM, ahjohannessen <ahjoha...@gmail.com> wrote:
Thanks for the tip :)

--
You received this message because you are subscribed to the Google Groups "FubuMVC Development Group" group.
To view this discussion on the web visit https://groups.google.com/d/msg/fubumvc-devel/-/WoAD9jYudZUJ.

To post to this group, send email to fubumv...@googlegroups.com.
To unsubscribe from this group, send email to fubumvc-deve...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/fubumvc-devel?hl=en.

Reply all
Reply to author
Forward
0 new messages