Search code examples
asp.net-mvcurl-routing

MVC routing with both ID and action at same level


I'd like to be able to make a MVC route that has both an action and the ID at the same level.

Example: If a call is made to

HTTP://www.something.com/F/GetMyData 

then the action GetMyData is called

If the call is made to

HTTP://www.something.com/F/dfeTD53F 

and no action is found with this name its assumed that "dfeTD53F" is an ID controller F should be called with that ID.

Is this possible and how?

Is this just bad application design?

Right now I have routes to all specific actions under F in my route.config, and that'll propably get quite messy in time.


Solution

  • My solution to this was creating a "catch all" route (as the final entry) and then handle it manually in the controller.

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

    Its not an elegant solution, but well.. MVC routing isnt elegant to begin with.