Add static segment for routing to make pagination more asp.net core friendly
On my website, I made my urls more friendly using bullets. I would like the search call related url information to be more friendly by changing
www.example.com/controller/action/1 // 1 represents the page
to
www.example.com/controller/action/page-1
I don't care if it's impossible, but I would like to.
I tried to decorate my action
[Route("action/page-{pagina?}")]
public IActionResult Action(int page= 1)
when navigating through it.
optional parameter "page" is preceded by an invalid page "<->
+3
source to share
2 answers
You can use an optional parameter with a constraint and then parse the parameter to get the page number
//Matches action
//Matches action/page-1
[Route("action/{page:regex(^page-\d$)?}")]
public IActionResult Action(string page = "page-1") {
//... parse page
// page number = page.Replace("page-", "");
// then int.Parse() page number
}
0
source to share
I did it successfully in mine Startup.cs
in Configure method
app.UseMvc(routes => {
/* code removed for brevity */
routes.MapRoute(
name: "RouteName",
template: "action/page-{pageNumber}",
defaults: new { Controller = "YourController", Action = "Action" }
);
});
Then your controller will be ...
public IActionResult Action(int page= 1)
It depends on whether you want to use attribute routing or not.
+1
source to share