简体   繁体   English

添加参数和向后兼容性?

[英]Adding parameters and backward compatibility?

Using WebAPI I have a restful service. 使用WebAPI,我有一个宁静的服务。

public SomeValue GetSomeValue()
{
}

I need to now pass in a string, but it's optional and a default value is fine: 我现在需要传递一个字符串,但是它是可选的,默认值可以:

public SomeValue GetSomeValue(string language="EN")
{
}    

Will old clients that I can't update send a call just to GetSomeValue() still work, with a default value sent in? 我无法更新的旧客户端将呼叫发送到GetSomeValue()仍然可以使用默认值吗? Or do I need to create a second method GetSomeValueForLanguage(string language) with GetSomeValue() calling it internally? 还是我需要使用内部调用GetSomeValue()方法来创建第二种方法GetSomeValueForLanguage(string language)

Change the method to take a default string parameter; 更改方法以采用默认字符串参数; old clients will be able to call it fine still, assuming your routing is staying the same (in which case language will be appended to the query string). 假设您的路由保持不变(在这种情况下, language会附加到查询字符串中),那么旧客户端仍可以调用它。 If you're adding language as a route token, ensure that it's optional on the route such that the default parameter value for the action is used. 如果要添加language作为路由标记,请确保该language在路由上是可选的,以便使用操作的默认参数值。

you can try this: 您可以尝试以下方法:

public classs  MyParams
{
   public string language{set;get;}
}

then change you method to this: 然后将您的方法更改为此:

public SomeValue GetSomeValue([FromBody] MyParams obj)
{
     if(String.IsNullOrEmpty(obj.language) obj.language="en";
     //you code
}

if your HTTP method is GET, you shall use FormUrl attribute instead of FromBody attribute 如果您的HTTP方法是GET,则应使用FormUrl属性而不是FromBody属性

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

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