Answered by:
ASP.NET advanced routing

Question
-
User-1496281956 posted
I can't seem to get advanced routing working in ASP.NET Web Pages I am trying to have two routes a page so it saves me time writing a lot of code.
Basically one url will be article/year/month/name
and another url will be /name (Just a pagename like termsofuse etc)
Both will be mapped to the same physical file on disk.
The other route will be archive and label.
I want the URL to be archive/year/month
and the other one to be /lablel/id
Both mapped to the same physical file, however I cannot get this to work! Been here all day and it really sucks, I have got the archieve and article working, but th other two shorter URLS do not work.
Here are the routes.
//labels RouteTable.Routes.MapWebPageRoute("{category}/{pYear}/{pMonth}", "~/views/archive.cshtml", new{category=-1,pYear=-1, pMonth=-1}); //normal pages. RouteTable.Routes.MapWebPageRoute("{PageName}", "~/views/article.cshtml", new {PageId=-1, PageYear=-1, PageMonth=-1, PageName=-1,}); //articles RouteTable.Routes.MapWebPageRoute("article/{PageId}/{PageYear}/{PageMonth}/{PageName}", "~/views/article.cshtml", new {PageId=-1, PageYear=-1, PageMonth=-1, PageName=-1,});
The primary error I get is the notorious.....OBJECT REFERENCE NOT SET TO AN INSTANCE OF AN OBJECT..
Friday, May 31, 2013 5:02 PM
Answers
-
User-821857111 posted
What version of the route handler package are you using? 0.2 included a null ref bug fix.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, June 1, 2013 3:02 PM
All replies
-
User-821857111 posted
Try changing the order:
//articles RouteTable.Routes.MapWebPageRoute("article/{PageId}/{PageYear}/{PageMonth}/{PageName}", "~/views/article.cshtml", new {PageId=-1, PageYear=-1, PageMonth=-1, PageName=-1,}); //normal pages. RouteTable.Routes.MapWebPageRoute("{PageName}", "~/views/article.cshtml", new {PageId=-1, PageYear=-1, PageMonth=-1, PageName=-1,}); //labels RouteTable.Routes.MapWebPageRoute("{category}/{pYear}/{pMonth}", "~/views/archive.cshtml", new{category=-1,pYear=-1, pMonth=-1});
Saturday, June 1, 2013 4:06 AM -
User-1496281956 posted
How come it has to go in a specific order, and how do you know the order it should go in?
Saturday, June 1, 2013 6:10 AM -
User-821857111 posted
URLs are matched to routes in the order in which the routes are added. As soon as a match is made, no other routes are tested. Your "labels" route matches any one, two or three segment URL because you provide default values for each parameter. If you did not provide defaults, only three segment URLs would match. You need to add the most specific routes first otherwise your parameters-only routes with default values start to act like a catch-all.
Saturday, June 1, 2013 6:43 AM -
User-1496281956 posted
Right ok, only one route does not work I fixed the rest. The order is article > page (single) > labels > archive.
The label route does not work.
//labels RouteTable.Routes.MapWebPageRoute("{category}/{Label}", "~/views/archive.cshtml"); //archive RouteTable.Routes.MapWebPageRoute("{category}/{pYear}/{pMonth}", "~/views/archive.cshtml", new{label=-1});
The archive route only works when label is set to -1 I don't know why.
Saturday, June 1, 2013 9:13 AM -
User-821857111 posted
What do you mean by "doesn't work"?
Saturday, June 1, 2013 12:49 PM -
User-1496281956 posted
Sorry I mean it givees this error:
Object reference not set to an instance of an object.
It points to line 57 in webpagesroutehandler.cs
The rest of the routes work fine.
Saturday, June 1, 2013 1:21 PM -
User-821857111 posted
What version of the route handler package are you using? 0.2 included a null ref bug fix.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, June 1, 2013 3:02 PM -
User-1496281956 posted
How do I find out the version?
Saturday, June 1, 2013 3:05 PM -
User-1496281956 posted
OK never mind updated it to 0.2 and yay it works, cheers Mike
Saturday, June 1, 2013 3:08 PM