简体   繁体   中英

Defining routes in asp.net Web API

I'm trying to define a Put api method with this syntax:

/api/orders/12/orderdate

where 12 is orderId, orderdate is an action method with 2 parameters: int orderId, OrderDate orderdate.

What could be the route definition for this method?

You can use this route:

config.Routes.MapHttpRoute(
            name: "Orders",
            routeTemplate: "api/{controller}/{orderId}/{action}"
        );

You can also be more explicit if you wish:

config.Routes.MapHttpRoute(
            name: "Update Order Date",
            routeTemplate: "api/orders/{orderId}/orderdate"
            defaults: new { controller = "Orders", action = "OrderDate" }
        );

And your action method would look like this:

[HttpPut]
public HttpResponseMessage OrderDate(int orderId, OrderDate orderDate)
{
    // implementation
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM