November Happy Hour will be moved to Thursday December 5th.

MVC Routing for a Listing Page

Vote:
 

Hi Everyone,

I've got a question to do with MVC Routing and pagination.

We have a Controller that defines a default 'Index' action method and a second 'Page' action method that takes a single parameter for the page to display. This works fine when we pass in the 'page' request parameter as expected.

For example, the following URL works fine:

/en-US/Services/News/Page?page=3

However, we would prefer to not use a request parameter but indicate the page using the url. Something like the following:
 
/en-US/Services/News/Page/3
 

 

We understand that we need to create a custom routing to do this so we started to do this as follows but couldn't quite make it work.routeCollection.MapRoute(

"NewsArticleListing",
".*/News/{action}/{id}",
new { controller = "NewsArticleListing", action = "Index", id = "6" }
);

Additionally, this is a multilingual website and the URL will change for each locale. For example, the URL will change 'News' will be 'Nieuws' for the Netherlands.  Does this mean we need to create a custom routing for each locale?

 
#72515
Jun 19, 2013 11:57
Vote:
 

Hi,

Would this be what you're looking for?

routes.MapContentRoute(
  name: "ActionWithPage",
  url: "{language}/{node}/{partial}/{action}/{page}",
  defaults: new { action = "Index", page = "1" }
);

That would let you pass in a page value to each action, for all routes and actions. You might be able to add a constraint to the route where you force "action" to be equal to "Page", and only then pass in the page number. Otherwise you might run into cases where this route matches all requests.

#72534
Jun 19, 2013 17:49
Vote:
 

Thanks Andreas, 

I'll give it a try but other priorities have come up so may be getting back on how successful I was :)

#72560
Jun 20, 2013 18:47
Vote:
 

Andreas. Thanks this seems to have done the trick. I needed to tweak our code a bit but otherwise this worked. Just so everyone else knows I had to wrap it in an if statement as per feedback from http://world.episerver.com/Modules/Forum/Pages/Thread.aspx?id=69513 

if (!ContentReference.IsNullOrEmpty(ContentReference.StartPage))

{
      routes.MapContentRoute(
      name: "ActionWithPage",
      url: "{language}/{node}/{partial}/{action}/{page}",
      defaults: new { action = "Index", page = "1" }
   );
}

#72576
Edited, Jun 21, 2013 11:00
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.