3

I have a public page that has client side routing. I would like all urls of the form Public/* to be routed to PublicController/Index.

I tried this:

routes.MapRoute(
                name: "Public",
                url: "Public/{clientRoute}",
                defaults: new { controller = "Public", action = "Index", id = UrlParameter.Optional }
            );

and this works for urls of type Public/someRoute but does not work for a url of the form Public/someRoute/secondRoute or for Public/someRoute/secondRoute/thirdRoute etc.

Tommy
  • 39,592
  • 10
  • 90
  • 121
pQuestions123
  • 4,471
  • 6
  • 28
  • 59
  • Not very sure what you are after. But if you are thinking about having variable number of url params, check this http://stackoverflow.com/questions/34573103/asp-net-mvc-5-single-controller-method-to-handle-paths-in-file-browser-mode/34573450#34573450 ? – Shyju Feb 01 '16 at 21:02
  • If you're using MVC > 5 then you can define the attribute routing on specific actions to support such behavior. – vendettamit Feb 01 '16 at 21:04
  • You can try using IIS module URLRewrite to rewrite the URL's. – Mark Feb 01 '16 at 21:18

1 Answers1

9

You were really close! This should take care of what you are looking to do.

routes.MapRoute(
                name: "Public",
                url: "Public/{*clientRoute}",
                defaults: new { controller = "Public", action = "Index"}
            );

Note the star in the URL parameter, this tells it to function as a "catch all". Now, anything starting with Public/ will go to this controller/action. Also, if you don't need the id parameter, don't concern yourself with specifying a default for it.

Tommy
  • 39,592
  • 10
  • 90
  • 121