动态使用MapHttpRoute

时间:2016-03-10 12:03:31

标签: c# .net asp.net-web-api

我正在寻找一种使用MapHttpRoute的动态方式 我的路线是:

    [HttpGet]
    [ActionName("adherence")]
    public AdherenceReport GetAdherenceReport(string clientId)  {  } 

    [HttpGet]
    [ActionName("adherence")]
    public AdherenceReport GetAdherenceReport(string clientId, int year, int month) {  }

目前我有两种方法:

r2/reports/client1/adherence
r2/reports/client1/adherence?year=2015&month=8
r2/reports/client1/adherence?month=8
r2/reports/client1/adherence?year=2015

目前,这两种方法都运行良好。我更喜欢只使用一种方法。 我希望能够支持不同类型的请求:

I1 = imread('coins.png');
I2 = imread('coins.png');
appendedIm = [I1,I2];

编辑:现在,只有第一个和第二个请求有效。第三个和第四个返回错误。

有可能吗?
感谢。

1 个答案:

答案 0 :(得分:2)

如果您将yearmonth参数设为可选(甚至可能是nullable),它将起作用:

[HttpGet]
[ActionName("adherence")]
public AdherenceReport GetAdherenceReport(string clientId)  {  } 

[HttpGet]
[ActionName("adherence")]
public AdherenceReport GetAdherenceReport(string clientId, int year = 0, int month = 0) {  }

在方法内部,您只需检查这些值并采取相应措施。

希望有所帮助。