开发者

How do I create a route for matching all paths starting with a given prefix?

开发者 https://www.devze.com 2023-04-09 22:28 出处:网络
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.

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
);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号