November Happy Hour will be moved to Thursday December 5th.
November Happy Hour will be moved to Thursday December 5th.
In asp.net mvc, if you have two routes with the same number of parameters and no constraints, first route will be called.
Can you check this thread: http://world.episerver.com/Modules/Forum/Pages/Thread.aspx?id=121971
Hi Dejan, i checked your thread, and i found your last post there interesting.. about adding constraints.
But i don't quite follow how to apply constraints in my case. How can i differentiate between my routes. As you can see in my PageContollerBase. i have my actions defined which are hit using matching url pattern.
I'm running latest EPiServer 8.8.x version at the moment.
Hope you can give some more insight in my case.
Hi,
Something like this?
public class CustomConstraint : IContentRouteConstraint { private readonly Type _contentType; private readonly string _actionName; private readonly IContentLoader _contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>(); public CustomConstraint(Type contentType, string actionName) { _contentType = contentType; _actionName = actionName; } public bool Match(Route route, SegmentContext segmentContext, string parameterName) { if (ContentReference.IsNullOrEmpty(segmentContext.RoutedContentLink)) { return false; } var content = _contentLoader.Get<IContent>(segmentContext.RoutedContentLink); // constraint 1: content types must match if (_contentType.IsInstanceOfType(content)) { // constraint 2: actions must match if (segmentContext.RouteData.Values.ContainsKey("action") && string.Equals(segmentContext.RouteData.Values["action"] as string, _actionName, StringComparison.CurrentCultureIgnoreCase)) { return true; } } return false; } } [InitializableModule] [ModuleDependency(typeof(ServiceContainerInitialization))] public class CustomRoutingModule : IInitializableModule { public void RegisterRoutes(RouteCollection routes) { RouteTable.Routes.MapContentRoute(null, "{language}/{node}/{action}/{name}/{pid}", new { action = "Index", name = UrlParameter.Optional, pid = UrlParameter.Optional }, new MapContentRouteParameters { Constraints = new { x = new CustomConstraint(typeof(SitePageData), "showcase") } }); RouteTable.Routes.MapContentRoute(null, "{language}/{node}/{action}/{name}/{sid}", new { action = "Index", name = UrlParameter.Optional, sid = UrlParameter.Optional }, new MapContentRouteParameters { Constraints = new { x = new CustomConstraint(typeof(SitePageData), "epib") } }); } public void Initialize(InitializationEngine context) { RegisterRoutes(RouteTable.Routes); } public void Uninitialize(InitializationEngine context) { } public void Preload(string[] parameters) { } }
I'm sure there's a much more easier / epi-friendly way to solve this, but this should do the trick in the meantime :)
awesome dejan...
i will dive into what exactly we are doing here, but i implemented this and it works... why do we have the action="Index" now for the routes? I thought the action name must match the action in my controller... How does that work?
C/p, sorry :)
You can remove action="Index", you don't need it (at least in version 8.8.1). It seems that action="something" doesn't have any effect here.
Hey guys,
i want some very basic routing like explained by Joel here in his article : http://joelabrahamsson.com/custom-routing-for-episerver-content/#basic-routing
now i created my route like this for a virtual page (external source):
so urls like http://abc.local/company/showcase/i-test-showcase/234 are getting routed to my controller , because it checks for the action name 'showcase' in the url.
Until here everything works great. I can do further rendering of my page in detail.cshtml.
Now i have another kinda virtual page (from another source) which has the same kinda url pattern only it should match on another action called EPIB. So now my routing is extended like so.
Now urls like http://abc.local/about/epib/a-new-case-epib/980 i would like to be hit by the new route and handled in the controller by its own action.
the controllers now is extended like so:
The problem as you can now see is that if i call my EPIB route, my sid is always null. If i switch my routes and put the EPIB route first then it works, but then my Showcase route doesnt work..
Am i missing something?