的ASP.NET Web API - 路由 [英] ASP.NET Web API - Routing
问题描述
我使用的Web API暴露了一堆的服务。我有问题,与一些路线和需要一些帮助。
I am using Web API to expose a bunch of services. I am having issue with some routes and need some help.
我的默认路由定义的:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new {id = RouteParameter.Optional}
);
通过这条路,我可以打正常途径,如:'/ API /客户/'和'/ API /客户/ 4。我想击中以下路线的GETAPI /客户/ 4 /配置文件和API /客户/ 4 /验证。
With this route I can hit normal routes such as: '/api/clients/' and '/api/clients/4'. I would like a GET that hits the following routes 'api/clients/4/profiles' and 'api/clients/4/validations'.
我曾尝试以下路线没有成功:
I have tried the following routes without success:
config.Routes.MapHttpRoute(
name: "ClientProfilesApi",
routeTemplate: "api/{controller}/{clientid}/profiles",
defaults: new { action = RouteParameter.Optional },
constraints: new { controller = "clients" }
);
config.Routes.MapHttpRoute(
name: "ClientValidationsApi",
routeTemplate: "api/{controller}/{clientid}/validations",
defaults: new { action = RouteParameter.Optional },
constraints: new { controller = "clients" }
);
我也用ActionName属性如下尝试:
I also tried using the 'ActionName' attribute as follows:
[HttpGet]
[ActionName("profiles")]
public IEnumerableResponseDto<ProfileLayoutDto> GetProfiles(Int64 clientId, [FromUri] IEnumerableRequestDto request)
{ .... }
[HttpGet]
[ActionName("profiles")]
public IEnumerableResponseDto<ValidationLayoutDto> GetValidations(Int64 clientId, [FromUri] IEnumerableRequestDto request)
{ .... }
我在想什么?它是不可能在一个控制器的多GET操作?
What am I missing? Is it not possible to have multiple GETs in a controller?
推荐答案
有关路线'API /客户/ 4 /配置文件和API /客户/ 4 /验证,命名行动轮廓和验证,然后请使用以下路由的缺省路由前:
For routes 'api/clients/4/profiles' and 'api/clients/4/validations', name the actions 'profiles' and 'validations' then use the following routes BEFORE the default route:
config.Routes.MapHttpRoute(
name: "ClientProfilesApi",
routeTemplate: "api/clients/{clientid}/profiles",
defaults: new { controller = "clients", action = "profiles", },
constraints: new {clientid = @"\d+" }
);
config.Routes.MapHttpRoute(
name: "ClientValidationsApi",
routeTemplate: "api/clients/{clientid}/validations",
defaults: new { controller = "clients", action = "validations", },
constraints: new {clientid = @"\d+" } );
这意味着航线API /客户/ 4 /型材云到控制器的'客户'和行动的个人资料和参数的clientid'必须是一个整数。
This means route 'api/clients/4/profiles' goes to controller 'clients' and action ' profiles' and that the parameter 'clientid' has to be an integer.
的默认路由应该永远是最后一次。
The default routes should ALWAYS be last.
这篇关于的ASP.NET Web API - 路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!