views:

248

answers:

4

If you review a SO question URL you will see that an ID and a "SLUG" are passed to the Questions controller: http://stackoverflow.com/questions/676934/what-do-you-need-to-write-your-own-blog-engine. What I find interesting is you can change the "SLUG" portion of the URL without affecting the application's ability to route the request example. The only way I could think to pull this off is have a route that accepted an id and a "SLUG" and used a route constraint on the slug to ensure it followed a pattern. I had to use a constraint to ensure that having the two variables didn't result in this route matching all requests. Does anyone have a better way to accomplish this, or any examples of more advanced routing scenarios?

ADDITION:

I realize the SLUG is for human readablity, and I would like to duplicate this feature in another application. What is the best way to accomplish this.

Route:

routes.MapRoute(
    "Id + Slug",          // Route name
    "Test/{id}/{slug}",   // URL with parameters
    new                   // Parameter defaults
    {
        controller = "Test", 
        action = "Details", 
        id = "", 
        slug = "" 
    },  
    new { slug = new SlugConstraint() }
);

Simple Constraint:

public class SlugConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext,
        Route route,
        string parameterName,
        RouteValueDictionary values,
        RouteDirection routeDirection)
    {
        string value = values[parameterName].ToString();

        return value.Contains("-");
    }
}
+6  A: 

You don't need to use the slug at all. It's probably just there for human readability and search engine optimization. You can ignore it when routing and just work up to the ID.

For instance, click here:

http://stackoverflow.com/questions/677158/

JoshJordan
A: 

The "route" can be anything the router can be configured to handle. The dynamic parts of the "route" are then passed to the landing/target page and retrieved via the RouteValue() method (in ASP.NET Routing). What you do with those values on the target page is entirely up to you. Use them or ignore them.

So the ID (in your example) is the key and the rest (the slug) is just there for human readability and SEO.

peiklk
Can you give an example of how the route entry might be structured?
JPrescottSanders
routes.Add(Routes.MEMBERS_PROFILE.Text(), new Route("members/profile/{userid}/{userfullname}", new PVRouteHandler("/member/profile.aspx")));
peiklk
+1  A: 

Is this what you're looking for...it doesn't define the slug at the end.

Kieron
A: 

The slug is there for search engines to catalog the resource/page. It's not used in the route at all as part of the arguments passed to the database to retrieve the requested post.

The ID is the important part.

So in your code, the SlugConstraint is not required and the value of the slug argument is ignored in the Details action.

This behavior is what SO do and what you can do, if u wish to copy SO.

Pure.Krome
I realize the slug isn't required, I wanted to know how to get the same functionality (for search engines)... the accepted answer led me to a solution for handling this sort of carry along data.
JPrescottSanders