I have the following route defined in my Global.asax (MVC 3 web project):
routes.MapRoute(
"BlogCategory", // Route name
"Blog/Category/{*category}", // URL with parameters
new { controller = "Blog", action = "Index", category = "" } // Parameter defaults
);
And my action accepts a category parameter, looking like this:
public ViewResult Index(string category, int page = 1)
{
PostListViewModel viewModel;
if (string.IsNullOrEmpty(category))
{
....show all cats else show only the one passed in
This works fine and will pass the category through to the controller, filtering my results appropriately.
My problem is when one of the categories I created looks like this for it's category name:
Projects / Lab
(note the spaces and slash)
This creates a URL similar like this:
/Blog/Category/Projects%20/%20Lab
And when I follow the link, I get this error:
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /Blog/Category/Projects / Lab
It never reaches the index action when debugging.
My question is, how can I make this work, or must I do some input validation when creating category names to prevent this from occurring?