Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jul 23, 2012 10:22 PM by Maran
Member
37 Points
24 Posts
Jul 23, 2012 02:05 PM|LINK
From My Home page I have Link to VideoGallery Page
On the VideoGallery page let say I have a following code
@{
var id =UrlData[0].AsInt();
var DB = Database.Open("DB_Name");
var sql = "SELECT * FROM VideoGallery Where VideoID =id";
var video = DB.QuerySingle(sql,id);
//on the same page its list all videos for Side Navigation which hold the list of videos
var listSql = "Select VideoTitle from VideoGallery";
var videoList = DB.Query(listSql);
}
Question
When User click on the VideoGallery from Home page, He is not providing an ID to retreive the page.
/VideoGallery/{id}
is there any way to make sql query to passes a default ID from Page? Or is there any way I could implement this requirements?
Thanks
All-Star
154858 Points
19858 Posts
Moderator
MVP
Jul 23, 2012 05:40 PM|LINK
var id =UrlData[0].AsInt(); if(id == 0){ id = default_value; } var sql = "SELECT * FROM VideoGallery Where VideoID = @0"; var video = DB.QuerySingle(sql,id);
Contributor
4846 Points
740 Posts
Jul 23, 2012 05:52 PM|LINK
You can set a default value for a route in your Global.asax file like so:
For Web Forms:
routes.MapPageRoute("", "VideoGallery/{id}", "~/VideoGallery.aspx", true, new RouteValueDictionary {{"id", "0"}});
If a value isn't passed for "id" then the id of "0" is used using the above code.
http://msdn.microsoft.com/en-us/library/cc668201.aspxLook for the heading "Setting Default Values for URL Parameters".
For MVC:
routes.Add(new Route ( "VideoGallery/{id}" new CategoryRouteHandler() ) { Defaults = new RouteValueDictionary {{"id", "0"}} } )
http://msdn.microsoft.com/en-us/library/cc668201.aspx Look for the "Setting Default Values for Route Parameters" heading.
Jul 23, 2012 07:36 PM|LINK
jprochazka For Web Forms... For MVC:
This is the Web Pages forum.
Jul 23, 2012 07:39 PM|LINK
Sorry about that then try this for web pages:
RouteTable.Routes.MapWebPageRoute("VideoGallery/{id}", "~/VideoGallery.cshtml", new { id = "0" });
A point of referance: http://mikesdotnetting.com/Article/187/More-Flexible-Routing-For-ASP.NET-Web-Pages
Nice article BTW. : )
Jul 23, 2012 08:36 PM|LINK
jprochazka Nice article BTW. : )
Thanks, but overkill for this scenario. UrlData is just fine: http://www.mikesdotnetting.com/Article/165/WebMatrix-URLs-UrlData-and-Routing-for-SEO
Jul 23, 2012 10:22 PM|LINK
Mike you once again you rocks.. thanks for the speedy reply.
Yes as mike said its for webpages.
Maran
Member
37 Points
24 Posts
URL routing
Jul 23, 2012 02:05 PM|LINK
From My Home page I have Link to VideoGallery Page
On the VideoGallery page let say I have a following code
@{
var id =UrlData[0].AsInt();
var DB = Database.Open("DB_Name");
var sql = "SELECT * FROM VideoGallery Where VideoID =id";
var video = DB.QuerySingle(sql,id);
//on the same page its list all videos for Side Navigation which hold the list of videos
var listSql = "Select VideoTitle from VideoGallery";
var videoList = DB.Query(listSql);
}
Question
When User click on the VideoGallery from Home page, He is not providing an ID to retreive the page.
/VideoGallery/{id}
is there any way to make sql query to passes a default ID from Page? Or is there any way I could implement this requirements?
Thanks
Mikesdotnett...
All-Star
154858 Points
19858 Posts
Moderator
MVP
Re: URL routing
Jul 23, 2012 05:40 PM|LINK
var id =UrlData[0].AsInt(); if(id == 0){ id = default_value; } var sql = "SELECT * FROM VideoGallery Where VideoID = @0"; var video = DB.QuerySingle(sql,id);Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
jprochazka
Contributor
4846 Points
740 Posts
Re: URL routing
Jul 23, 2012 05:52 PM|LINK
You can set a default value for a route in your Global.asax file like so:
For Web Forms:
routes.MapPageRoute("", "VideoGallery/{id}", "~/VideoGallery.aspx", true, new RouteValueDictionary {{"id", "0"}});If a value isn't passed for "id" then the id of "0" is used using the above code.
http://msdn.microsoft.com/en-us/library/cc668201.aspx
Look for the heading "Setting Default Values for URL Parameters".
For MVC:
routes.Add(new Route ( "VideoGallery/{id}" new CategoryRouteHandler() ) { Defaults = new RouteValueDictionary {{"id", "0"}} } )If a value isn't passed for "id" then the id of "0" is used using the above code.
http://msdn.microsoft.com/en-us/library/cc668201.aspx
Look for the "Setting Default Values for Route Parameters" heading.
Mikesdotnett...
All-Star
154858 Points
19858 Posts
Moderator
MVP
Re: URL routing
Jul 23, 2012 07:36 PM|LINK
This is the Web Pages forum.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
jprochazka
Contributor
4846 Points
740 Posts
Re: URL routing
Jul 23, 2012 07:39 PM|LINK
Sorry about that then try this for web pages:
RouteTable.Routes.MapWebPageRoute("VideoGallery/{id}", "~/VideoGallery.cshtml", new { id = "0" });A point of referance:
http://mikesdotnetting.com/Article/187/More-Flexible-Routing-For-ASP.NET-Web-Pages
Nice article BTW. : )
Mikesdotnett...
All-Star
154858 Points
19858 Posts
Moderator
MVP
Re: URL routing
Jul 23, 2012 08:36 PM|LINK
Thanks, but overkill for this scenario. UrlData is just fine: http://www.mikesdotnetting.com/Article/165/WebMatrix-URLs-UrlData-and-Routing-for-SEO
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
Maran
Member
37 Points
24 Posts
Re: URL routing
Jul 23, 2012 10:22 PM|LINK
Mike you once again you rocks.. thanks for the speedy reply.
Yes as mike said its for webpages.