Routing using database values

Editor
Dec 15, 2012 at 4:53 PM
Edited Dec 15, 2012 at 4:56 PM

I got these two routes in the database in the _AppStart

 

 

  RouteTable.Routes.MapWebPageRoute("{rcPageName}", "~/Default.cshtml", new {rcPageName = "default"});
  RouteTable.Routes.MapWebPageRoute("tutorials/{culture}/{RouteName}/{rcPageName}", "~/Default.cshtml",  new {culture=-1, RouteName = -1, rcPageName = "default" });

 

Then in the default.cshtml I got this:

 

 var selectQueryString = "";


    //check what page is requested and load data from database
    var pageName = Context.GetRouteValue("rcPageName");
    if (pageName == null) { pageName = "default"; }

    var culture = Context.GetRouteValue("culture");
    var routename = Context.GetRouteValue("RouteName");
    if (culture == null){culture = "en-gb";}


    var db = Database.Open("rcthecodingguys");


    selectQueryString = "SELECT pId, pName, pTitle, pText, mTitle, mDescription, mKeywords, pMasterPage, pBody, pEditDate "
                               + "FROM rc_Pages "
                               + "WHERE pCulture=@0 AND pRouteName=@1 AND pName=@2 ";

                     

var data = db.QuerySingle (selectQueryString, culture, routename, pageName);

 

 

The issue I am having is that when users go to URL like mysite.com/sitemap or mysite.com/ I get error object reference not set to instance of object. and it will point to the two additional routes I added.

 

Also having the culture in the URL is optional so mysite.com/tutorials/en-gb/csharp/pagename the "en-gb" is not required but if I remove it I get an error. (404 Page)

 

On the new razorC version the routes are like this:

 

 RouteTable.Routes.MapWebPageRoute("{rcPageName}/{rc0}/{rc1}", "~/Default.cshtml", new { rcPageName = "default", rc0=-1,rc1=-1});

 

This route works nicely but not what I am looking for!

Editor
Dec 15, 2012 at 11:15 PM
Edited Dec 15, 2012 at 11:16 PM

Edit I have done this now:

 

In _AppStart

 
  RouteTable.Routes.MapWebPageRoute("{rcPageName}", "~/Default.cshtml", new {culture="en-gb", RouteName="page", rcPageName = "default", });
  RouteTable.Routes.MapWebPageRoute("tutorials/{culture}/{RouteName}/{rcPageName}", "~/Default.cshtml",  new {culture="en-gb", RouteName = "page", rcPageName = "default" });

IN Default page

 

   var selectQueryString = "";
    var db = Database.Open("rcthecodingguys");

    //check what page is requested and load data from database

    var culture = Context.GetRouteValue("culture");
    if (culture == null){culture = "en-gb";}

    var routename = Context.GetRouteValue("RouteName");
    if (routename == null){routename = "page";}

    var pageName = Context.GetRouteValue("rcPageName");
    if (pageName == null) { pageName = "default"; }

    



 

    selectQueryString = "SELECT pId, pName, pTitle, pText, mTitle, mDescription, mKeywords, pMasterPage, pBody, pEditDate "
                               + "FROM rc_Pages "
                               + "WHERE pCulture=@0 AND pRouteName=@1 AND pName=@2 ";


     var data = db.QuerySingle (selectQueryString, culture, routename,  pageName);    

 

Now it works for URLs like http://mysite.com/page but if its a tutorial page like mysite.com/tutorials/csharp/pagename it will not work because the culture route was not specifed it will work if the url is like  mysite.com/tutorials/en-gb/csharp/pagename

 

I wanted the culture route constraint to be optional I tried this as well

 

RouteTable.Routes.MapWebPageRoute("tutorials/{culture}/{RouteName}/{rcPageName}", "~/Default.cshtml",  new {culture=-1, RouteName = "page", rcPageName = "default" });

 

But it  did not work, says the data is null. 

Coordinator
Dec 18, 2012 at 7:24 PM

I think you need to change order, put  culture at the end if this one is optional or maybe use session or cookie to keep info about culture...

when you look at routing in razorC, you will see 2 parameters that are optional rc0 and rc1 - you can use them for paging or anything else but they are optional 

RouteTable.Routes.MapWebPageRoute("{rcPageName}/{rc0}/{rc1}", "~/Default.cshtml", new { rcPageName = "default", rc0=-1,rc1=-1});