I have enabled automated routing for my website, in the _AppStart I have something like this:
RouteTable.Routes.MapWebPageRoute("{rcPageName}", "~/Default.cshtml", new { rcPageName = "default" });
RouteTable.Routes.MapWebPageRoute("tutorials/php/{rcPageName}", "~/Default.cshtml", new { rcPageName = "php-tutorial"});
RouteTable.Routes.MapWebPageRoute("tutorials/visualbasic/{rcPageName}", "~/Default.cshtml", new { rcPageName = "vb-tutorial" });
Instead of doing this for all the tutorials I have, can I just add a new column to the table and let routing be done automatically, the only thing which needs replacing is the tutorials/<tutorialname> and at the end rcPageName= "";
//check what page is requested and load data from database
var pageName = Context.GetRouteValue("rcPageName");
var routeName = Context.GetRouteValue("cat");
if (pageName == null) { pageName = "default"; }
var db = Database.Open("db");
var selectQueryString = "SELECT pId, pName, pTitle, pText, mTitle, mDescription, mKeywords, pMasterPage, pBody, pEditDate "
+ "FROM rc_Pages "
+ "WHERE pName = @0 AND pRouteName=@1";
var data = db.QuerySingle (selectQueryString, pageName, routeName);
If the page does not need a route it is not left blank it has the value "N/A"
The above code is just like the MapRoute which we have for a HomeController in an ASP.NET MVC Project
Like this
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
Thanks
With Regards
Abhishek Rajiv Luv
"Helpful then please Mark as Answer"
http://www.codeabstract.com/
http://pluralsight.com/training/users/abhishekluv
I cannot get it to work, this is the code I have in the Default.cshtml
//check what page is requested and load data from database
var pageName = Context.GetRouteValue("rcPageName");
var routename = Context.GetRouteValue("pRouteName");
if (pageName == null) { pageName = "default"; }
var db = Database.Open("db");
selectQueryString = "SELECT pId, pName, pTitle, pText, mTitle, mDescription, mKeywords, pMasterPage, pBody, pEditDate "
+ "FROM rc_Pages "
+ "WHERE pName=@0 AND pRouteName=@1";
var data = db.QuerySingle (selectQueryString, pageName, routename);
And this is the code in the _AppStart
RouteTable.Routes.MapWebPageRoute("{rcPageName}/{rc0}/{rc1}", "~/Default.cshtml", new { rcPageName = "default", rc0=-1,rc1=-1});
RouteTable.Routes.MapWebPageRoute("tutorials/{pRouteName}/{rcPageName}", "~/Default.cshtml", new {pRouteName =-1, rcPageName="default",});
I get error : Object reference not set to an instance of an object. On this line in the Default.cshtml:
var routename = Context.GetRouteValue("pRouteName");
All for my information the error Object reference not set to an instance of an object. Is because of the reason that you are not having any data in the value that you are using.
Please try to check that all of your variables are having some value.
Please "Marks As Answer" if any answer helped you out!
~~! FIREWALL !~~
All the pages are data driven..I been trying a lot of solutions I tried this one now.
RouteTable.Routes.MapWebPageRoute("{rcPageName}", "~/Default.cshtml", new { rcPageName = "default"});
RouteTable.Routes.MapWebPageRoute("tutorials/{RouteName}/{rcPageName}", "~/Default.cshtml", new { rcPageName = "default", RouteName=-1});
This one works really good the only issue I have is that users will be able to access tutorial pages like mysite.net/tutorial page
and they will be able to maniuplate the <routename> so instead of tutorials/asp/page they can put tutorials/anything/page name, this might be a security issue and WILL be bad for SEO
I am seriously not able to understand why you want to use this type of URL
The URL tutorials/asp/page will open a link as tutorials is the web page then a folder asp then the page.
You can suppose tutorial.com as the domain then asp the folder than page as the file. like http://www.tutorial.com/asp/page
The browser will also even open the page if the page is asp for example the site I am working on has a folder for user than a file stream. But I want to open the profile of a user with his unique ID So I do my work as localhost:7320/user/stream/27 the 27
is an additional page. there is no folder as stream and there is no page as 27. But I can still use this page.
I will get the ID from URL as
var url = UrlData[0];
This will give me the page. Now If I have to get the images or for example the language of the user I can edit the Url as localhost:7320/user/stream/27/urdu To get this I will user
CriticalErro...
Member
423 Points
394 Posts
Automated routing, through database.
Dec 07, 2012 03:25 PM|LINK
I have enabled automated routing for my website, in the _AppStart I have something like this:
RouteTable.Routes.MapWebPageRoute("{rcPageName}", "~/Default.cshtml", new { rcPageName = "default" }); RouteTable.Routes.MapWebPageRoute("tutorials/php/{rcPageName}", "~/Default.cshtml", new { rcPageName = "php-tutorial"}); RouteTable.Routes.MapWebPageRoute("tutorials/visualbasic/{rcPageName}", "~/Default.cshtml", new { rcPageName = "vb-tutorial" });Instead of doing this for all the tutorials I have, can I just add a new column to the table and let routing be done automatically, the only thing which needs replacing is the tutorials/<tutorialname> and at the end rcPageName= "";
How should I do something like this?
My Site | My Blog
CriticalErro...
Member
423 Points
394 Posts
Re: Automated routing, through database.
Dec 07, 2012 04:18 PM|LINK
Update:
I have managed to do this and it works fine:
RouteTable.Routes.MapWebPageRoute("{rcPageName}", "~/Default.cshtml", new {rcPageName = "default" }); RouteTable.Routes.MapWebPageRoute("tutorials/{cat}/{rcPageName}", "~/Default.cshtml", new {rcPageName = "default"});However, I get the usual error:
Object reference not set to an instance of an object.
When trying to access a page which does not follow the format /tutorials/<category>/page.
My Site | My Blog
Afzaal.Ahmad...
Contributor
2661 Points
1040 Posts
Re: Automated routing, through database.
Dec 07, 2012 05:58 PM|LINK
This one CriticalError is because your SQL statement (The table) May not have the value which you are using here.
You can try to use
if(result != null) { // Your work.. }This way only code will run if the result is not null.
Or else you can achieve that only row. To see whether its fertile or not.
~~! FIREWALL !~~
CriticalErro...
Member
423 Points
394 Posts
Re: Automated routing, through database.
Dec 07, 2012 06:05 PM|LINK
URLS like this will work
http://localhost:24/tutorials/visualbasic/vb-select-case
URLs like this will not work:
http://localhost:24/sitemap
http://localhost:24
In the _AppStart I have this code now:
RouteTable.Routes.MapWebPageRoute("{rcPageName}", "~/Default.cshtml", new {rcPageName = "default" }); RouteTable.Routes.MapWebPageRoute("tutorials/{cat}/{rcPageName}", "~/Default.cshtml", new{cat = ""});In the Default.cshtml I have this code:
//check what page is requested and load data from database var pageName = Context.GetRouteValue("rcPageName"); var routeName = Context.GetRouteValue("cat"); if (pageName == null) { pageName = "default"; } var db = Database.Open("db"); var selectQueryString = "SELECT pId, pName, pTitle, pText, mTitle, mDescription, mKeywords, pMasterPage, pBody, pEditDate " + "FROM rc_Pages " + "WHERE pName = @0 AND pRouteName=@1"; var data = db.QuerySingle (selectQueryString, pageName, routeName);If the page does not need a route it is not left blank it has the value "N/A"
My Site | My Blog
Abhishek Luv
Participant
1746 Points
468 Posts
Re: Automated routing, through database.
Dec 08, 2012 01:21 AM|LINK
The above code is from the Latest Webmatrix CMS called razorC CMS.
RouteTable.Routes.MapWebPageRoute("{rcPageName}/{rc0}/{rc1}", "~/Default.cshtml",new { rcPageName = "default", rc0=-1,rc1=-1});The above code is just like the MapRoute which we have for a HomeController in an ASP.NET MVC Project
Like this
routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
With Regards
Abhishek Rajiv Luv
"Helpful then please Mark as Answer"
http://www.codeabstract.com/
http://pluralsight.com/training/users/abhishekluv
Afzaal.Ahmad...
Contributor
2661 Points
1040 Posts
Re: Automated routing, through database.
Dec 08, 2012 06:26 AM|LINK
the sitemap URL you are using. Should be a page.
Because sitemap is a format for a file too. Browser thinks might you are using that file. But not so thats why it say no file present.
~~! FIREWALL !~~
CriticalErro...
Member
423 Points
394 Posts
Re: Automated routing, through database.
Dec 08, 2012 11:13 AM|LINK
I cannot get it to work, this is the code I have in the Default.cshtml
//check what page is requested and load data from database var pageName = Context.GetRouteValue("rcPageName"); var routename = Context.GetRouteValue("pRouteName"); if (pageName == null) { pageName = "default"; } var db = Database.Open("db"); selectQueryString = "SELECT pId, pName, pTitle, pText, mTitle, mDescription, mKeywords, pMasterPage, pBody, pEditDate " + "FROM rc_Pages " + "WHERE pName=@0 AND pRouteName=@1"; var data = db.QuerySingle (selectQueryString, pageName, routename);And this is the code in the _AppStart
RouteTable.Routes.MapWebPageRoute("{rcPageName}/{rc0}/{rc1}", "~/Default.cshtml", new { rcPageName = "default", rc0=-1,rc1=-1}); RouteTable.Routes.MapWebPageRoute("tutorials/{pRouteName}/{rcPageName}", "~/Default.cshtml", new {pRouteName =-1, rcPageName="default",});I get error : Object reference not set to an instance of an object. On this line in the Default.cshtml:
var routename = Context.GetRouteValue("pRouteName");My Site | My Blog
Afzaal.Ahmad...
Contributor
2661 Points
1040 Posts
Re: Automated routing, through database.
Dec 08, 2012 02:02 PM|LINK
All for my information the error Object reference not set to an instance of an object. Is because of the reason that you are not having any data in the value that you are using.
Please try to check that all of your variables are having some value.
~~! FIREWALL !~~
CriticalErro...
Member
423 Points
394 Posts
Re: Automated routing, through database.
Dec 08, 2012 03:08 PM|LINK
Its the pRouteName which may cause the error, because most URLs go in this format
http://sitename.net/tutorials/<langauge>/tutoral page
Others go like this
http://sitename.net/<page>
All the pages are data driven..I been trying a lot of solutions I tried this one now.
RouteTable.Routes.MapWebPageRoute("{rcPageName}", "~/Default.cshtml", new { rcPageName = "default"}); RouteTable.Routes.MapWebPageRoute("tutorials/{RouteName}/{rcPageName}", "~/Default.cshtml", new { rcPageName = "default", RouteName=-1});This one works really good the only issue I have is that users will be able to access tutorial pages like mysite.net/tutorial page
and they will be able to maniuplate the <routename> so instead of tutorials/asp/page they can put tutorials/anything/page name, this might be a security issue and WILL be bad for SEO
My Site | My Blog
Afzaal.Ahmad...
Contributor
2661 Points
1040 Posts
Re: Automated routing, through database.
Dec 08, 2012 04:03 PM|LINK
I am seriously not able to understand why you want to use this type of URL
The URL tutorials/asp/page will open a link as tutorials is the web page then a folder asp then the page.
You can suppose tutorial.com as the domain then asp the folder than page as the file. like http://www.tutorial.com/asp/page
The browser will also even open the page if the page is asp for example the site I am working on has a folder for user than a file stream. But I want to open the profile of a user with his unique ID So I do my work as localhost:7320/user/stream/27 the 27 is an additional page. there is no folder as stream and there is no page as 27. But I can still use this page.
I will get the ID from URL as
This will give me the page. Now If I have to get the images or for example the language of the user I can edit the Url as localhost:7320/user/stream/27/urdu To get this I will user
This will get the second data from the page.
I hope you get my point.
http://www.mikesdotnetting.com/Article/165/WebMatrix-URLs-UrlData-and-Routing-for-SEO
This website of Mike Brind is a helpfull site. You can get help from this without any question in mind!
~~! FIREWALL !~~