0

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

Royi Namir
  • 144,742
  • 138
  • 468
  • 792

1 Answers1

1

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 RouteHandler and 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)

public class CustomRouteHandler : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        string virtualPath = "~/path/to/page.aspx";

        int id;

        // this would obviously be some sort of database call
        if (requestContext.RouteData.Values["Name"] == "Paul") 
        {
            id = 2;
        }
        else
        {
            id = 8675309;
        }

        string newPath = string.Format(
            "{0}?id={1}",
            virtualPath,
            id
        );


        HttpContext.Current.RewritePath(newPath);

        return BuildManager.CreateInstanceFromVirtualPath(virtualPath, typeof(Page)) as IHttpHandler;
    }
}

And to register the route

RouteTable.Routes.Add("MyCustomRoute", new Route("Music/{Band}/{Name}", new CustomRouteHandler());
Community
  • 1
  • 1
Matthew
  • 24,703
  • 9
  • 76
  • 110
  • What about my other solution ? ( inject the id into the URL like SO does ) ? – Royi Namir Dec 18 '12 at 19:49
  • 1
    You would make a custom route handler to do that. – Matthew Dec 18 '12 at 19:52
  • you aren't clear :-). when injecting the id into the url - i dont need to scan the db. BUT when writing only paul and scanning DB - this is totaly another thing. what should I do ? – Royi Namir Dec 18 '12 at 20:00
  • Take a look at http://dotnet.dzone.com/news/iroutehandler-aspnet-mvc, you would modify the code in `GetHttpHandler` to perform the Paul-to-2 conversion, and load up the asp page with the proper arguments. – Matthew Dec 18 '12 at 20:06
  • Sorry.:-) but how would i do it in the web-form example ? ( where). thanks. – Royi Namir Dec 18 '12 at 20:12
  • I gave a better example in my original example, I'm not sure if it compiles, however. – Matthew Dec 18 '12 at 20:27
  • P.s. - please notice that if I want to call a page with pauls data - i will need extra round trip to DB - just to get Paul's value .Also it will require me that the name column in the DB will be unique.I think the best solution is to add the id into the URL - just like StackOverflow does. and so - I will be able to enjoy both worlds...(not enforcing unique column , got already the ID , friendly url) what do you say ? – Royi Namir Dec 19 '12 at 08:34
  • I agree, for projects I work on, I just use the ID in urls, for example: `http://mysite.com/2-Beatles-Paul` where you both have a textual URL and the ID in it. – Matthew Dec 19 '12 at 14:28