My first thought is that is a 'bad idea'. If you can take anything they can throw at you for a URL, you're going to have to have a heck of a blacklist (or whitelist). There's so many openings. A better way would be to explicitly spell out those routes and how you allow parameters, and then have actions that deal with those accepted routes. You'd then have a generic-catch all route that would redirect to an error page.
It feels like you're trying to mix apples and oranges. ASP.NET MVC purposefully does away with the 'page' idea. There's really no reason to have directories for various users unless you're doing some sort of File I/O, and if that's the case then it can be abstracted to work in the ASP.NET MVC paradigm much more easily than you may think.
In ASP.NET MVC if you wanted to vary the information you're looking for based on the string passed (much like the string you passed), here's a 'safe' way of doing it:
Method #1 - Three Routes, Three Actions, Different Names
routes.MapRoute(
"YearOnly",
"{year}",
new { controller = "Index", action = "ShowByYear" },
new { year = @"\d{4}" }
);
routes.MapRoute(
"YearAndMonth",
"{year}/{month}",
new { controller = "Index", action = "ShowByYearAndMonth" },
new { year = @"\d{4}", month = @"\d{2}" }
);
routes.MapRoute(
"YearMonthAndName",
"{year}/{month}/{name}",
new { controller = "Index", action = "ShowByYearMonthAndName" },
new { year = @"\d{4}", month = @"\d{2}" }
);
And then you'd use the passed route values in your Controller Actions to determine how they see the data:
ShowByYear(string year)
{
//Return appropriate View here
}
ShowByYearAndMonth(string year, string month)
{
//Return appropriate View here
}
ShowByYearMonthAndName(string year, string month, string name)
{
//Return appropriate View here
}
Method #2 - Suggested Method
routes.MapRoute(
"YearOnly",
"{year}",
new { controller = "Index", action = "Show" },
new { year = @"\d{4}" }
);
routes.MapRoute(
"YearAndMonth",
"{year}/{month}",
new { controller = "Index", action = "Show" },
new { year = @"\d{4}", month = @"\d{2}" }
);
routes.MapRoute(
"YearMonthAndName",
"{year}/{month}/{name}",
new { controller = "Index", action = "Show" },
new { year = @"\d{4}", month = @"\d{2}", name = "" }
);
Show(string year)
{
//
}
Show(string year, string month)
{
//Return appropriate View here
}
Show(string year, string month, string name)
{
//Return appropriate View here
}
The beauty of this approach is that the MapRoute
handles URL parsing; and keeps the riff-raff out. You can then set up a catch-all route that just throws an error. You'd rather have the some parsing done on the route side with regex
than in your controller (in my opinion).
This keeps it down to three overloaded actions, and allows for cleaner and more 'MVC'-ish code.