Pregunta

Using WebAPI I have a restful service.

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? Or do I need to create a second method GetSomeValueForLanguage(string language) with GetSomeValue() calling it internally?

¿Fue útil?

Solución

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). 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.

Otros consejos

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top