Cannot pass object of type 'RouteCollectionRoute' to inject 'Routing.Route'
I am trying to add a {culture} token to all generated urls.
Unable to run object of type 'System.Web.Mvc.Routing.RouteCollectionRoute' for input of type 'System.Web.Routing.Route'.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
using (routes.GetReadLock())
{
foreach (Route r in routes)
{
if (!(r.RouteHandler is SingleCultureMvcRouteHandler))
{
r.RouteHandler = new MultiCultureMvcRouteHandler();
r.Url = "{culture}/" + r.Url;
//Adding default culture
if (r.Defaults == null)
{
r.Defaults = new RouteValueDictionary();
}
r.Defaults.Add("culture", Culture.en.ToString());
//Adding constraint for culture param
if (r.Constraints == null)
{
r.Constraints = new RouteValueDictionary();
}
r.Constraints.Add("culture", new CultureConstraint(Culture.en.ToString(),
Culture.nl.ToString()));
}
}
}
}
I have a MapCultureRoute method that creates two routes, one with the {culture} prefix, but that doesn't work with attribute routing.
MapCultureRoute:
public static void MapCultureRoute(string name, string url, object defaults, object constraints = null)
{
var c = CultureHelper.GetDefaultCulture();
var d = new ExpandoObject() as IDictionary<string, object>;
var t = defaults.GetType();
var pi = t.GetProperties();
foreach (var p in pi)
{
d.Add(p.Name, p.GetValue(defaults, null));
}
//map route with specified culture
RouteTable.Routes.MapRoute(
name: name,
url: "{culture}/" + url,
defaults: defaults,
constraints: constraints
);
//map route with default culture
RouteTable.Routes.MapRoute(
name: name + "-DefaultCulture",
url: url,
defaults: d,
constraints: constraints
);
}
+3
source to share
No one has answered this question yet
Check out similar questions: