Lets say I have this sql server table :
id Name Band
--------------------
1 John Beatles
2 Paul Beatles
3 George Beatles
4 Ringo Beatles
5 Jim Doors
I want to have a person details page like :
http://localhost/Music/Beatles/Paul
and I want to treat is as :
http://localhost/Details.aspx?id=2
And so- I’m writing this code to register a simple route :
void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapPageRoute("MyRoute", "Music/{Band}/{Name}", "~/Details.aspx");
}
But I don’t understand :
When Asp.net get http://localhost/Music/Beatles/Paul , how does he know it knows that Paul’s value is 2 ?
Are you telling me that each time asp.net encounters Paul he should go to DB and scan a pre-defined unique column (Name in this case) and get it’s column ID value?
Or Should I inject Paul’s value into the url like SO doing :
http://stackoverflow.com/questions/13938994/jquery-conditional-validation-based-on-select-text
^
|
---------------------------------------
which will be in my case :
http://localhost/Music/Beatles/2/Paul
Asp.net does not intrinsicly know that Paul’s ID is
2.You would have to write this logic somewhere yourself. You could write your own
RouteHandlerand do the routing yourself (which would do the db call to determine the proper ID).Personally, I would just let the aspx page handle the information itself and not assume it is given an actual ID.
Here’s a code sample of a custom route handler (taken from Friendly URLs for ASP.NET)
And to register the route