Join us this Friday for AI in Action at the Virtual Happy Hour! This free virtual event is open to all—enroll now on Academy and don’t miss out.

 

Custom MVC Routing with EPiServer 7

Vote:
 

Hi all

Really hoping someone can help me with this one. Im working on a version 7 MVC site. I have a page which it has its own template, but that page can also be loaded from else where in the site through a modal window, in this case it will have different HTML markup, there are also two options for the data that is displayed on that page, this option will toggle between a couple of properties available on the page.

So what I am trying to do is add a custom route to do two things:

  1. Call a seperate method from my controller in order to call a different view
  2. Pass a variable in another segment in the URL to control the toggling of properties

The perfect URL structure would give me a URL structure like the following (where node is the page name alias from EPiServer):

www.mysite.com/{node}/{variable}/{method}/

Although I would still be very happy just with:

www.mysite.com/{node}/{method}/{variable}/

With the default routing in EPiServer the following works:

www.mysite.com/{node}/{method}

However as soon as you add the variable to the end, I get a 404. I have tried adding the following custom route to the RegisterRoutes override in the Global.asax file:

routes.MapRoute(

     "ModalLayout", // Route name

     "{node}/{action}/{myvariable}/", // URL with parameters

     new { controller = "MyController", action = "Index", myvariable = "defaultvalue" });

I have tried several variations of the above including adding the controller to the URL but I get 404 errors with everything I try. Can anyone recommend the best way to approach this?

Many Thanks
Kind Regards

Dave

#73441
Jul 22, 2013 10:35
Vote:
 
#73455
Jul 22, 2013 19:27
Vote:
 

If you want to keep your routing within the EPiServer context, you'll need to use MapContentRoute instead of the standard MVC MapRoute. I think for your particular case, I'd suggest keeping it in the EPiServer context.

I'm guessing you've checked out Joel's article on custom routing (http://joelabrahamsson.com/custom-routing-for-episerver-content/). There's also this forum post that I've found helpful (http://world.episerver.com/Modules/Forum/Pages/thread.aspx?id=65422).

What other things have you tried?

#73456
Jul 22, 2013 19:27
Vote:
 

Hi Chris 

Thank you very much for your response. It rurned out when I changed to use:

RouteTable.Routes.MapContentRoute

Instead of the RouteCollection it started working straight away. In case anyone else comes across this with a similar problem, my solution was:

RouteTable.Routes.MapContentRoute(
     "ModalLayout", 
     "{language}/{node}/{action}/{variable}", 
     new { action = "modal" });

Many thanks for your help Chris

Kind Regards
Dave

#73465
Jul 23, 2013 11:43
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.