How to make more MapHttpRoutes for MVC 4 Api

前端 未结 1 2061
长发绾君心
长发绾君心 2021-02-04 07:17

I have 2 API routes for my API atm, but I want to add more, and the way I am doing it it seems to overwrite each other, so in the code I pasted, only the CreateUser

1条回答
  •  别跟我提以往
    2021-02-04 07:56

    I believe the pattern api/{controller}/{cUser} in "CreateUser" route is matching with rest of the controller actions because of its more generic pattern. Use specific controller name in the routes as "User" (api/User/{cUser}) and "Game" (api/Game/{playerId}). The more specific routes should be at the top and more generic at the bottom.

    routes.MapHttpRoute(
        name: "CreateUser",
        routeTemplate: "api/User/{cUser}",
        defaults: new
        {
            controller = "User",
            action = "CreateUser",
            cUser = RouteParameter.Optional
        }
    );
    
    routes.MapHttpRoute(
        name: "AllGames",
        routeTemplate: "api/Game/{playerId}",
        defaults: new
        {
            controller = "Game",
            action = "GetAllGames",
            playerId = RouteParameter.Optional
        }
    );
    

    0 讨论(0)
提交回复
热议问题