3

I have a RESTful web service, built with ASP.NET WebAPI 2.

I have this method in a controller:

[Route("{DocNum:int}")]
public object Patch(int DocNum, string str = null)
{
    if(str == null)
    {
        //do something when parameter has NOT been passed...
    }
    else
    {
        //do something when parameter has been passed...
    }
}

If I don't pass str, it is null in the method.

If I pass str=abc, it is "abc" in the method.

If I pass str= (empty string), it is null in the method.

That is ASP.NET WebAPI 2 treats empty string query parameters as null!

It seems it is by design, but is there a way to treat an empty string as an empty string?

3
  • Why do you want to treat it as an empty string? Commented Feb 1, 2019 at 17:51
  • @mason To distinguish when the client wants to set a field to empty string, for cleaning it for instance (it should pass str=), from when he doesn't want to touch the field (it should not pass str at all). Commented Feb 1, 2019 at 17:55
  • I dont think this is possible. Can you not send any placeholder value which you can compare on server side and treat it as string.empty ? Commented Feb 1, 2019 at 18:03

2 Answers 2

1

There is no such thing as null in HTML. Input has either some value or empty value. There isn't even a way to tell if the value is a string or a number just by looking at the query parameter.

The default behavior of HTML form is to include all fields on submit. So even if the input has no value it will still be included as part the query. www.example.com/xxx?str= and www.example.com/xxx are both valid syntax to represent that there is no value entered for str field.

You can however include a hidden field

<input name="IsEmptyString" type="hidden"/>

in your form and use JavaScript to set the value based on whatever logic you use to determine if it's empty or null.

Sign up to request clarification or add additional context in comments.

2 Comments

OK, thanks!... Finally I decided to restructure my REST method from a list of optional parameters to just 2 parameters, FieldName and FieldValue, so that there is no ambiguity.
I had another case regarding this issue and this time I found a great solution, the accepted answer.
1

I found this great solution, copied from https://stackoverflow.com/a/35966463/505893 and improved. It is a customization in the global configuration of the web app.

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        //treat query string parameters of type string, that have an empty string value,
        //(e.g. http://my/great/url?myparam=) as... empty strings!
        //and not as null, which is the default behaviour
        //see https://stackoverflow.com/q/54484640/505893
        GlobalConfiguration.Configuration.BindParameter(typeof(string), new EmptyStringModelBinder());

        //...
    }
}

/// <summary>
/// Model binder that treats query string parameters that have an empty string value
/// (e.g. http://my/great/url?myparam=) as... empty strings!
/// And not as null, which is the default behaviour.
/// </summary>
public class EmptyStringModelBinder : System.Web.Http.ModelBinding.IModelBinder
{
    public bool BindModel(HttpActionContext actionContext, System.Web.Http.ModelBinding.ModelBindingContext bindingContext)
    {
        var vpr = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        if (vpr != null)
        {
            //parameter has been passed
            //preserve its value!
            //(if empty string, leave it as it is, instead of setting null)
            bindingContext.Model = vpr.AttemptedValue;
        }

        return true;
    }
}

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.