How to achieve a dynamic controller and action method in ASP.NET MVC?

前端 未结 3 1998
遇见更好的自我
遇见更好的自我 2020-12-02 14:26

In Asp.net MVC the url structure goes like

http://example.com/{controller}/{action}/{id}

For each \"controller\", say http://example.com/blog, there is a Bl

3条回答
  •  执念已碎
    2020-12-02 15:28

    After a little more reflection, there may be a bit simpler way for you to handle the dynamic action names than my other answer. You'll still need to override the default controller factory. I think you could define your route like:

    routes.MapRoute("Dynamic", "{controller}/{command}/{id}", new { action = "ProcessCommand" });
    

    Then on your default/dynamic controller you'd have

    public ActionResult ProcessCommand(string command, int id)
    {
       switch(command)
       {
          // whatever.
       }
    }
    

提交回复
热议问题