This project is read-only.

Routing questions

Oct 1, 2011 at 4:10 PM

Hi Rishi,

- Is there a function to convert an UrlRequest to a qualified navigation string ([SiteArea];[Url] -> nRoute format)

which includes the sitearea, the url and the (html escaped) parameters....

- Is there a function to parse a qualified navigation string to get the sitearea, url and parameters?

- Is it possible to use query syntax with routes? (eg.   /articles?search=30  for route /articles/{search} )

- Does the routine engine html encode/decode routing parameters?

Many Thanks!

Best regards,


Oct 4, 2011 at 4:26 PM

Hi Helmut, let me start by saying good questions.. I had to dig deep into the code to get you the answers :)

Firstly, about prefixing the Urls with the SiteArea is something I did try when I first introduced/create SiteAreas - and though I can't now remember what specific issues I ran into, it did lead to tons of issues so much so that I had to revert back to the non-prefixed code-base. Now, I might still give it a try to do that as I think it makes a lot of sense, especially when triggering the Url from external sources. Also my approach was sort of like [SiteArea]://articles/21/  <- so there the SiteArea became the protocol, this made it compatible with the standard Uri format, rather than [SiteArea];[Url] scheme.

Second, is there a function available to parse sitearea, url, parameters as one - well, not really, with one single exception - in cases where we do so-called "Browser-Shell integration" there I use the [SiteArea];[Url] kind of pairing. Have a look at NavigationShellIntegration behavior.

Third, no, those two are not substitutable - for the most part I eschewed the idea of query-parameters with that of passing navigation-parameters. However, that does mean urls are not as portable as they can be; but then again it gave us the ability to pass .NET / complex types when navigating, which I thought is more useful that interfacing with external systems using Urls. Thinking about it again though, what we have looks like POST - which somehow doesn't exclude the possibility to still have regular query parameters. So it might something that could be introduced and integrated into the pipeline.

Lastly, no, but I think that should be the correct behaviour - so I might need to introduce that. 

Thanks for asking tough questions.


Oct 5, 2011 at 11:54 PM
Edited Oct 6, 2011 at 12:14 AM

Hi Rishi,

many thanks for your detailed answer.

It would be really helpful if nRoute could offer helpers out of the box to build/parse fully qualified navigation/routing urls
(IUrlRequests <-> [SiteArea]:[Url]  ...maybe something like the MVC UrlHelper class).

The benefits would be:
- It's standardized through the library...
- I can expose fully qualified urls to the user from a navigation request
- The user can fill up any navigation containers with navigation requests from qualified urls...
- I can programmatically build qualified urls...

Btw: HttpServerUtility.UrlTokenEncode would be also a great addition to the library to help to make the url parameters more safe
and readable.

Thanks for listening...and please keep up the good work!

Best regards,

Oct 7, 2011 at 12:46 AM

Hi Rishi,

after digging through the source code in the namespace Components.Routing and doing some tests i found a quick and cheap way of how to enable url building for routes:

1) Introduce the VirtualPathData type


    public class VirtualPathData
        private ParametersCollection _dataTokens;

        public VirtualPathData(RouteBase route, string virtualPath)
            this._dataTokens = new ParametersCollection();
            this.Route = route;
            this.VirtualPath = virtualPath;

        public ParametersCollection DataTokens 
                return _dataTokens;

        public RouteBase Route { get; set; }
        public string VirtualPath { get; set; }


2) Uncomment the lines 110-130 in Route.cs, change RouteDictionary parameter type to ParametersCollection

3) Uncomment the line 22 in RouteBase.cs, change RouteDictionary parameter type to ParametersCollection

4) Comment the lines 215-228 in ParsedRoute.cs to ensure that unused route parameters are not appended as a query string (maybe exception?)

5) Use it....

            var defaultParameters = new ParametersCollection
                { new Parameter { Key = "article", Value = "jeans" }, new Parameter { Key = "size", Value = 30 } };

            var routeHandler = new NavigationContentHandler(p => new MainPage());

            RoutingService.MapRoute("inventory/{article}/{size}/{*catchall}", defaultParameters, routeHandler);

            var requestParameters = new ParametersCollection
                    new Parameter { Key = "article", Value = "shorts" },
                    new Parameter { Key = "catchall", Value = "length/width" }

            var urlRequest = new UrlRequest("inventory/clothes");

            var routeData = RouteTable.Routes.GetRouteData(urlRequest);
            var routingContext = new RoutingContext(urlRequest, routeData, requestParameters);

            var virtualPath = routeData.Route.GetVirtualPath(routingContext, requestParameters);

            // virtualPath.VirtualPath -> "inventory/shorts/30/length/width"

The routing engine has perfectly build up the routing url (for the urlRequest and custom requestParameters) by

- replacing "clothes" with "shorts",

- inserting the default size "30 and

- appending the catchall "length/width".

Would it be possible to get these modifications as soon as possible into the library?
Many Thanks!

Best regards,