根据参数类型重载网页API操作方法 [英] Overload web api action method based on parameter type

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

问题描述

有没有要执行的操作方法,基于参数类型重载的方法吗?
即是否有可能做一个控制器以下

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.

如果你是它使用标准的路由尝试,你可能会需要路由到正确的操作通过它的名字,然后使用前章的约束(如的 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" }
        );

更新

我通常会使用一个POST,如果做一个FromBody(也许用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" }
            );

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

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