如何通过“控制器?动作和参数”的路由实现.NET WebAPI?

问题描述 投票:0回答:1

我正在尝试使用.NET Standard WebAPI实现特定的HTTP接口,要求所有路由都采用以下形式:

http://server:port/somepath/controller?action&parameter1=value1&parameter2=value2 ...

示例:

GET

http://www.google.com/myapi/api?getStuff&stuffId=1&otherParameter=2

POST,PUT

http://www.google.com/myapi/api?putStuff&stuffId=1&otherParameter=2

是否可以通过问号和参数之后的操作来执行此特定路由?

c# asp.net asp.net-web-api asp.net-web-api2 asp.net-web-api-routing
1个回答
0
投票

mapping your routes应该可以的:

routes.MapHttpRoute(
    name: "ActionApi",
    routeTemplate: "api/{controller}?{action}&{id}&{parameter1}",
    defaults: new { id = RouteParameter.Optional }
);

查询参数可以添加到您的控制器操作中:

public HttpResponseMessage Get(int id, string parameter)
{
   //...
}
© www.soinside.com 2019 - 2024. All rights reserved.