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

1

1 Answer 1

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());
Sign up to request clarification or add additional context in comments.

8 Comments

What about my other solution ? ( inject the id into the URL like SO does ) ?
You would make a custom route handler to do that.
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 ?
Take a look at 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.
Sorry.:-) but how would i do it in the web-form example ? ( where). thanks.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.