user161433
user161433

Reputation: 4519

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

Upvotes: 0

Views: 1374

Answers (1)

Daniel A. White
Daniel A. White

Reputation: 191037

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.

Upvotes: 3

Related Questions