How do I create a route for matching all paths starting with a given prefix?
In my MVC application I want to create a route such that when a user requests a URL starting with a prefix some specific action is invoked.
For example, I want a route that would map processData{whatever}
onto an action so that when a user requests processData
, processData.asmx
or processDataZOMG
or whatever else with processData
prefix that action is invoked.
I tried the following route
routes.MapRoute(
@"ProcessData", @"processData*", //<<<< note asterisk
new { controller = @"Api", action = @"ProcessData" } );
but it doesn't match processData
and anything with that prefix - route matching falls through and the request is redirected to the main page.
How do I make a route that matches all开发者_JS百科 paths with a specific prefix onto a specific controller-action pair?
Try the following: Update: This solution does not work, please refer to the solution I offer in my comment to this answer.
routes.MapRoute(
@"ProcessData", @"processData/{*appendix}", //<<<< note asterisk
new { controller = @"Api", action = @"ProcessData" } );
You could use route constraints:
routes.MapRoute(
"ProcessData", // Route name
"{token}", // URL with parameters
new { controller = "Api", action = "ProcessData" }, // Parameter defaults
new { token = @"^processdata.*" } // constraints
);
精彩评论