ASP.net MVC routing with optional first parameter

前端 未结 4 1550
南笙
南笙 2020-12-08 23:32

I need to provide following functionality for one of the web sites.

http://www.example.com/[sponsor]/{controller}/{action}

Depending on the

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-09 00:07

    In addition to adding a second route before the default route, as Sergey said in his answer, you also must add a RouteConstraint to the initial route, to enforce that the {sponsor} token is the name of a valid sponsor.

    You can use the RouteConstraint in this answer: Asp.Net Custom Routing and custom routing and add category before controller

    Remember that you must also enforce a rule that a sponsor name cannot be the same as any of your controller names.

提交回复
热议问题