0

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";

1

1 Answer 1

0

As per Michael comment, I took a look into ModalBinder. I was able to resolve my issue. Below is the sample solution I used

public class ReqStringModelBinder : IModelBinder
{
    public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
    {
        if (bindingContext.ModelType != typeof(ReqString))
        {
            return false;
        }

        ValueProviderResult val = bindingContext.ValueProvider.GetValue(
            bindingContext.ModelName);
        if (val == null)
        {
            return false;
        }

        string key = val.RawValue as string;
        if (key == null)
        {
            bindingContext.ModelState.AddModelError(
                bindingContext.ModelName, "Wrong value type");
            return false;
        }

        ReqString result;
        if (ReqString.TryParse(key, out result))
        {
            bindingContext.Model = result;
            return true;
        }

        bindingContext.ModelState.AddModelError(
            bindingContext.ModelName, "Cannot convert value to ReqString");
        return false;
    }
}

[ModelBinder(typeof(ReqStringModelBinder))]
public class ReqString
{
    public string value { get; set; }
    public static bool TryParse(string s, out ReqString result)
    {
        result = null;
        result = new ReqString() { value = String.IsNullOrEmpty(s) ? "" : s };
        return true;
    }

    public static implicit operator string(ReqString reqString) => reqString.value;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.