This requires custom code to make a decision. The convention is {controller}/{action} which maps to {class}/{method}. The framework tries to find the Controller then an Action within the controller.
You could write custom route middleware that looks for an Action when the controller is not found. I do not recommend this approach. Just create a Go, Run, and Stop controller.
One of possible option to handle this is Route Constraint, you need to register a route constraint and each time route matches you will check the valid path and if you return true this route get executed otherwise you can do some other action.
None
0 Points
4 Posts
Optional routes
Nov 23, 2019 08:33 PM|islaboy|LINK
How can I make a route that accepts optional path or sub directory. Something like this:
this would be a valid route
{path}/go
{path}/run
{path}stop
and so is this which also point to a same controller and action as above.<br>
/go
/run
/stop
routes.MapRoute(
name: "Go",
url: "go",
defaults: new { controller = "Controller", action = "Go", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Go",
url: "path/go",
defaults: new { controller = "Controller", action = "Go", id = UrlParameter.Optional }
);
so basically i'm avoiding to write it twice like this.
All-Star
53631 Points
23988 Posts
Re: Optional routes
Nov 24, 2019 01:54 PM|mgebhard|LINK
This requires custom code to make a decision. The convention is {controller}/{action} which maps to {class}/{method}. The framework tries to find the Controller then an Action within the controller.
You could write custom route middleware that looks for an Action when the controller is not found. I do not recommend this approach. Just create a Go, Run, and Stop controller.
Contributor
2096 Points
1040 Posts
Re: Optional routes
Nov 25, 2019 12:29 PM|Khuram.Shahzad|LINK
One of possible option to handle this is Route Constraint, you need to register a route constraint and each time route matches you will check the valid path and if you return true this route get executed otherwise you can do some other action.