Search code examples
asp.net-mvcroutesslug

ASP.NET MVC - Regex for a URL Slug


I am writing a URL Slug for routing. Should I leave the slug constraint blank or what?

Here is my code:

routes.MapRoute(
                null,
                "q/{slug}/{id}",
                new { controller = "q", action = "Index" },
                new { id = @"^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$" }
            );

The slug will be in this format

how_do_you_open_jar_file_on_computer

Solution

  • I would leave it blank there. I would leave it up to the Action in the Controller to determine if its valid or not. That way you can easily throw 404 errors or redirect to default pages.