I have Web API which throws an error if one of the parameters passed in a URL query is null. Let's say I cant overwrite anything to the above validation.
So that, I want to have a Web API as below, the parameter c is string and the API call may pass an empty string which instantiates parameter c as null and not "".
[Route("{id}")]
[HttpGet]
public IdDTO GetIdDetails(
int Id,
[FromUri] int a,
[FromUri] string b,
[FromUri] string c,
{
//doing something here...
}
A typical API call is localhost:3000/123?a=1&b=abc&c=
I also changed the data type of c to a custom datatype ReqString ([FromUri] ReqString <string> c) as below
public struct ReqString<T>
{
private T _value;
public ReqString(T s)
{
_value = s;
}
public static implicit operator ReqString<T>(T s) => new ReqString<T>(s);
public static implicit operator string(ReqString<T> s)
{
if(typeof(T).Equals(typeof(string)))
{
return s._value as string == null ? "" : s._value as string;
}
else
{
return s._value.ToString();
}
}
}
The problem now is, the value for c is "". But if I pass values for parameter c in API URL query, it is still "" and not the value passed
for eg: localhost:3000/123?a=1&b=abc&c=def
ie, the _value is still null
So my question is How can I use implicit operators to instantiate _value with query value?
UPDATE
The custom datatype works when instantiated in a statement as below, but I want to get similar result in a function call parameter
ReqString rStr = "testing";