简体   繁体   English

REST API可选参数

[英]REST API Optional parameters

I build a Web Role with rest api. 我使用rest api构建一个Web角色。 All its arguments should be optional with default values 它的所有参数都应该是具有默认值的可选参数

I tried this: 我尝试了这个:

  [WebGet(UriTemplate = "RetrieveInformation/param1/{param1}/param2/{param2}/param3/{param3}")]
    public string RetrieveInformation(string param1, string param2, string param3)
    {

    }

I want that it will work for the following scenarios: 我希望它适用于以下情况:

https://127.0.0.1/RetrieveInformation/param1/2  

https://127.0.0.1/RetrieveInformation/param1/2/param3/3 

How can I do that?Will the below work? 我该怎么办?以下工作吗?

[WebGet(UriTemplate = "RetrieveInformation/param1/{param1=1}/param2/{param2=2}/param3/{param3=3}")]

I don't think that you can achieve this when using segments (ie using /). 我认为使用段(即使用/)时无法实现此目的。 You could use a wildcard character but it only allows you to do so for the last segment. 您可以使用通配符,但只允许在最后一段使用通配符。

[WebGet(UriTemplate = "RetrieveInformation/param1/{param1}/{*param2}")]

If you don't really need segments and can use query parameters, the following route will work 如果您确实不需要细分并且可以使用查询参数,则可以使用以下路线

[WebGet(UriTemplate = "RetrieveInformation?param1={param1}&param2={param2}&param3={param3}")]

Such a route will give you flexibility as parameters do not need to be ordered, nor are required. 这样的路线将为您提供灵活性,因为不需要订购参数,也不是必需的。 This will work with the following 这将适用于以下情况

http://localhost:62386/Service1.svc/GetData?param1=1&param2=2&param3=3
http://localhost:62386/Service1.svc/GetData?param1=1&param3=3&param2=2
http://localhost:62386/Service1.svc/GetData?param1=1&param2=2
http://localhost:62386/Service1.svc/GetData?param1=1&param3=3

You can find more information about UriTemplate @ http://msdn.microsoft.com/en-us/library/bb675245.aspx 您可以在http://msdn.microsoft.com/zh-cn/library/bb675245.aspx中找到有关UriTemplate的更多信息。

Hope this helps 希望这可以帮助

The param1 may not be optional when a param2 is defined, so doing this in a single route might be a bit cumbersome (if even possible). 当定义了param2时,param1可能不是可选的,因此在单个路由中执行此操作可能会比较麻烦(即使可能的话)。 It might be better to split your GET into multiple routes. 最好将GET分成多个路由。

Something like the below code might work better for you... 像下面的代码可能更适合您...

[WebGet(UriTemplate = "RetrieveInformation")]
public string Get1()
{
    return RetrieveInfo(1,2,3);
}

[WebGet(UriTemplate = "RetrieveInformation/param1/{param1}")]
public string GetP1(int param1)
{
    return RetrieveInfo(param1,2,3);
}

[WebGet(UriTemplate = "RetrieveInformation/param1/{param1}/param2/{param2}")]
public string GetP1P2(int param1, int param2)
{
    return RetrieveInfo(param1,param2,3);
}

[WebGet(UriTemplate = "RetrieveInformation/param1/{param1}/param3/{param3}")]
public string GetP1P3(int param1, int param3)
{
    return RetrieveInfo(param1,2,param3);
}

private string RetrieveInfo(int p1, int p2, int p3)
{
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM