1

For a specific controller, is it possible to route any action that does not exist to the index?

For example if I have

fashionController/
fashionController/shoes/
fashionController/bags/
fashionController/otherStuff/

I want to be able to only setup the Index view & action and that anything else will just use the Index automatically without having to create separate views/actions for anything else.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Pmb Denton
  • 13
  • 4

1 Answers1

0

Yes.

There is no requirement that controller or action name are part of Url. For example you can route all "fashion/*" to the same action with following route.

routes.MapRoute(
            "AllToIndex",
            "fashion/{argument1}",
            new { controller = "fashion", action = "Index", argument1 = "" }
        );

Note that routes matched in order they are added, so if you register this route after default "{controller}/{action}" one it will never be matched. Generally more specific routes should go before more generic once and last should be optional "cath'em all" one with "{*path}" math.

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • 1
    I think you mean `"{*path}"` for the catch-all route. – Eilon Apr 25 '15 at 21:30
  • @Eilon indeed. Thanks for pointing it out. Also added link to question that describes it in more details. – Alexei Levenkov Apr 25 '15 at 21:39
  • ok wow that works really well, however is it possible to mix it, so if the action/view exists for the controller it will go there, otherwise it will default to the catchall Index? – Pmb Denton Apr 25 '15 at 21:39
  • @PmbDenton I personally would explicitly specify routes I want to send to actions (either separately or with constraint on single route). There are probably other ways to do so - there are plenty different questions/articles on ASP.Net MVC routing if basic matching stops working for your needs. I.e. there is specific tag on SO for this kind of questions - http://stackoverflow.com/questions/tagged/asp.net-mvc-routing – Alexei Levenkov Apr 25 '15 at 21:44