the more I think about it the more I believe it\'s possible to write a custom route that would consume these URL definitions:
{var1}/{var2}/{var3}
Const/{var1}/{
Well. It cannot be in default hierarchy. 'cause, Routing layer splitted from actions. You cannot manipulate parameter bindings. You have to write new ActionInvoker or have to use RegEx for catching.
Global.asax:
routes.Add(new RegexRoute("Show/(?.*)/(?[\\d]+)/(?.*)",
new { controller = "Home", action = "Index" }));
public class RegexRoute : Route
{
private readonly Regex _regEx;
private readonly RouteValueDictionary _defaultValues;
public RegexRoute(string pattern, object defaultValues)
: this(pattern, new RouteValueDictionary(defaultValues))
{ }
public RegexRoute(string pattern, RouteValueDictionary defaultValues)
: this(pattern, defaultValues, new MvcRouteHandler())
{ }
public RegexRoute(string pattern, RouteValueDictionary defaultValues,
IRouteHandler routeHandler)
: base(null, routeHandler)
{
this._regEx = new Regex(pattern);
this._defaultValues = defaultValues;
}
private void AddDefaultValues(RouteData routeData)
{
if (this._defaultValues != null)
{
foreach (KeyValuePair pair in this._defaultValues)
{
routeData.Values[pair.Key] = pair.Value;
}
}
}
public override RouteData GetRouteData(System.Web.HttpContextBase httpContext)
{
string requestedUrl =
httpContext.Request.AppRelativeCurrentExecutionFilePath.Substring(2) +
httpContext.Request.PathInfo;
Match match = _regEx.Match(requestedUrl);
if (match.Success)
{
RouteData routeData = new RouteData(this, this.RouteHandler);
AddDefaultValues(routeData);
for (int i = 0; i < match.Groups.Count; i++)
{
string key = _regEx.GroupNameFromNumber(i);
Group group = match.Groups[i];
if (!string.IsNullOrEmpty(key))
{
routeData.Values[key] = group.Value;
}
}
return routeData;
}
return null;
}
}
Controller:
public class HomeController : Controller
{
public ActionResult Index(string topics, int id, string title)
{
string[] arr = topics.Split('/')
}
}