How to map WebAPI routes correctly

前端 未结 3 1530
抹茶落季
抹茶落季 2020-12-24 13:51

I\'m building an API for a Twitter like site using Web API and have trouble with mapping the routes

I have the following actions for the User controller:

<         


        
3条回答
  •  长发绾君心
    2020-12-24 14:19

    There are a variety of useful reference materials on this subject, such as:

    • Routing Basics with Web-API
    • Web-API Routing for multiple methods
    • Routing in Asp.net Mvc 4 and Web Api

    Have you had a look at these?

    Update..

    Its better practise to explicitly state which parameter is which, ie:

        config.Routes.MapHttpRoute(
            name: "2",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { action = "Get", id = RouteParameter.Optional },
        );
    
        config.Routes.MapHttpRoute(
            name: "1",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: null
        );
    

    Main thing I could see wrong was you had action / id in the wrong order in route "1".

提交回复
热议问题