基于参数类型的重载web api动作方法 [英] Overload web api action method based on parameter type

查看:19
本文介绍了基于参数类型的重载web api动作方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法对 Action 方法执行基于参数类型的重载?即是否可以在控制器中执行以下操作

Is there a way to perform parameter type based overloading of an Action method ? i.e. Is it possible to do the following in a Controller

public class MyController : ApiController
{
   public Foo Get(int id) { //whatever }

   public Foo Get(string id) { //whatever }

   public Foo Get(Guid id)  { //whatever }
}

如果是,需要对路由表进行哪些更改.

If so, what changes need to be made to the Route table.

推荐答案

这种场景在标准路由方法中并没有得到很好的支持.

This kind of scenario is not well supported by the standard routing methods.

您可能希望改用基于属性的路由,因为这为您提供了更大的灵活性.

You may want to use attribute based routing instead as this gives you a lot more flexibility.

具体看可以按类型路由的路由约束:

Specifically look at the route constraints where you can route by the type:

// Type constraints
[GET("Int/{x:int}")]
[GET("Guid/{x:guid}")]

任何其他事情都会变成一个黑客......例如

Anything else will turn into a bit of a hack... e.g.

如果您确实尝试使用标准路由,则可能需要通过其名称路由到正确的操作,然后使用 reg ex 的约束(例如 guid) 路由到所需的默认操作.

If you did attempt it using standard routing you would probably need to route to the correct action via it's name, then use reg ex's constraints (e.g. guid) to route to the required default action.

控制器:

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 与模型一起使用),但可以通过添加以下内容来满足您的要求.

I would normally use a POST if doing a FromBody (perhaps use the FromUri with the model instead) but your requirements could be met by adding the following.

对于控制器

    [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" }
            );

这篇关于基于参数类型的重载web api动作方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆