Asp.net web api 基于参数类型的重载webapi动作方法
有没有一种方法可以对动作方法执行基于参数类型的重载? i、 e.是否可以在控制器中执行以下操作Asp.net web api 基于参数类型的重载webapi动作方法,asp.net-web-api,Asp.net Web Api,有没有一种方法可以对动作方法执行基于参数类型的重载? i、 e.是否可以在控制器中执行以下操作 public class MyController : ApiController { public Foo Get(int id) { //whatever } public Foo Get(string id) { //whatever } public Foo Get(Guid id) { //whatever } } 如果是这样,需要对路由表进行哪些更改。标准路由方法
public class MyController : ApiController
{
public Foo Get(int id) { //whatever }
public Foo Get(string id) { //whatever }
public Foo Get(Guid id) { //whatever }
}
如果是这样,需要对路由表进行哪些更改。标准路由方法不能很好地支持这种情况 您可能想改用,因为这给了您更多的灵活性 具体查看可以按类型布线的布线约束:
// Type constraints
[GET("Int/{x:int}")]
[GET("Guid/{x:guid}")]
其他任何东西都会变成一个黑客。。。e、 g
如果您确实尝试使用标准路由,您可能需要通过其名称路由到正确的操作,然后使用reg ex的约束(例如)路由到所需的默认操作
控制器:
public class MyController : ApiController
{
[ActionName("GetById")]
public Foo Get(int id) { //whatever }
[ActionName("GetByString")]
public Foo Get(string id) { //whatever }
[ActionName("GetByGUID")]
public Foo Get(Guid id) { //whatever }
}
//Should match /api/My/1
config.Routes.MapHttpRoute(
name: "DefaultDigitApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { action = "GetById" },
constraints: new { id = @"^\d+$" } // id must be digits
);
//Should match /api/My/3ead6bea-4a0a-42ae-a009-853e2243cfa3
config.Routes.MapHttpRoute(
name: "DefaultGuidApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { action = "GetByGUID" },
constraints: new { id = @"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$" } // id must be guid
);
//Should match /api/My/everything else
config.Routes.MapHttpRoute(
name: "DefaultStringApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { action = "GetByString" }
);
路线:
public class MyController : ApiController
{
[ActionName("GetById")]
public Foo Get(int id) { //whatever }
[ActionName("GetByString")]
public Foo Get(string id) { //whatever }
[ActionName("GetByGUID")]
public Foo Get(Guid id) { //whatever }
}
//Should match /api/My/1
config.Routes.MapHttpRoute(
name: "DefaultDigitApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { action = "GetById" },
constraints: new { id = @"^\d+$" } // id must be digits
);
//Should match /api/My/3ead6bea-4a0a-42ae-a009-853e2243cfa3
config.Routes.MapHttpRoute(
name: "DefaultGuidApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { action = "GetByGUID" },
constraints: new { id = @"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$" } // id must be guid
);
//Should match /api/My/everything else
config.Routes.MapHttpRoute(
name: "DefaultStringApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { action = "GetByString" }
);
已更新
如果执行FromBody,我通常会使用POST(可能会将FromUri与模型一起使用),但是可以通过添加以下内容来满足您的需求
对于控制器
[ActionName("GetAll")]
public string Get([FromBody]MyFooSearch model)
{
if (model != null)
{
//search criteria at api/my
}
//default for api/my
}
//should match /api/my
config.Routes.MapHttpRoute(
name: "DefaultCollection",
routeTemplate: "api/{controller}",
defaults: new { action = "GetAll" }
);
您可以按如下方式编写方法代码
[Route("api/My/{id:int}")]
public string Get(int id)
{
return "value";
}
[Route("api/My/{name:alpha}")]
public string Get(string name)
{
return name;
}
[Route("api/My/{id:Guid}")]
public string Get(Guid id)
{
return "value";
}
作为一种解决方法,您可以定义一个接受字符串参数的方法,然后根据该参数可以解析为的内容将其委托给帮助器方法。@Joanna:这正是我目前正在做的。按参数类型将其分开的原因是为了生成Web API帮助文档,以便可以适当地记录每个操作,并且调用方知道要传递的有效参数类型in@AbhijeetPatel嗯,那些评论看起来像是破坏了东西;)-在JavaScription中,我被这一点弄糊涂了。如果我没有错,您可以使所有方法的参数名唯一,那么它应该按原样工作,即使方法名为
Get(string id1)
或Get(Guid id2)
。从客户端,您可以将其称为http://servername/My/Get?id1=23
或http://servername/My/Get?id2=3006ADFE-9597-4C71-8DDC-C12C10A4 FCBB
。因此,你不必给出唯一的动作名称。马克:这太棒了,谢谢!我还有另外两个基于GET的方法。一个“public Foo[]Get(){}”,用于获取类型为“Foo”的所有对象;一个“public Foo[]Get([FromBody]MyFooSearch search criteria){}”,用于根据调用者可以在正文中传递的筛选对象搜索Foo对象。我该如何设置实现上述目标的路线?@Mark Yes,谢谢。另一个不相关的注意事项是,似乎无法将请求主体中的复杂对象传递给我在上面定义的接受搜索条件的Get方法。Fiddler允许我这样做,但我不能通过Jquery/Ajax或通过.NET4.5中的任何HttpClient API这样做。这是否得到支持?我读过一些解决方案,其中有人将此作为POST实现,但这与RESTful服务的本质背道而驰。@AbhijeetPatel看看这个答案和Roy F的评论,基本上你应该将你的请求作为POST(并将搜索视为一种资源类型,即创建一个新的搜索,返回结果)或者做可能最自然的事情,使用查询参数,即Uri中的所有信息。这里有更多的辩论,太好了。我将[FromBody]改为[FromUri],这就成功了。通过将筛选条件作为Uri中的查询字符串值传递,这些值将按预期反序列化到“MyFooSearch”对象中。如果我没有错,您可以使所有方法的参数名唯一,然后它应该按原样工作,即使方法名为get(字符串id1)或get(Guid id2)。从客户端,您可以将其称为或4FCBB。因此,您不必给出唯一的操作名称。