2

I was wondering what is the best way to go about routing? I have a Index method like:

public IActionResult Index(int parentId = -1, int page = 0){
    ...
    return(model);
}

so since this action has default values, I would like the user be able to get to this action using controller, contoller/index, controller/parent/{parentId}, controller/page/{page}, controller/{parentId}/page/{page}. there are still more variations with [action] in the beginning which I have omitted.

so I have to make these routes (give or take) :

[Route("[action]/department/{parentId:int}/page/{page=0?int}")]
[Route("department/{parentId:int}/page/{page=0?int}")]
[Route("[action]/page/{page=0?int}")]
[Route("page/{page=0?int}")]
[Route("[action]")]
[Route("")]

And I was thinking if I get to have more parameters, this list could go a lot longer.

I feel there is something that I'm missing because this approach doesn't feel right.

I would like ask you to point me to a better direction. thanks

shaahin
  • 108
  • 1
  • 8
  • The fact that you have to have so many variations should be a sign that this action has too many responsibilities. think about a redesign. try to keep it simple. – Nkosi Dec 10 '16 at 19:55
  • well this action only is going to list all children of a parent ( like category and department) and pagination. so you are suggesting to move the pagination to another action? – shaahin Dec 10 '16 at 19:58
  • Keep pagination as optional. move action by parentid to another action where parentid is not optional. break up the variations into specific actions that will have their specific routes. you route expectations for that one action is too broad which will lead to problems down the road when it comes to maintainability and possible route conflicts – Nkosi Dec 10 '16 at 20:04
  • sure I will try this, just one more question is there a way to use same action name for all of these? since they both have int attribute, I dont assume I can use the same name. – shaahin Dec 10 '16 at 20:09
  • Action name does not matter if you are using attribute routing. You just need to make sure the route does not conflict with other routes – Nkosi Dec 10 '16 at 20:32

0 Answers0