Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Treat empty query string parameters as empty strings, without using a class for parameters

I am trying to pass multiple parameters to a httpget web api function. The key problem I am having is that empty query string parameters are being converted to null.

I can solve this by creating a class like something below:

public class CuttingParams
{
    [DisplayFormat(ConvertEmptyStringToNull = false)]
    public string batch_number { get; set; }
    [DisplayFormat(ConvertEmptyStringToNull = false)]
    public string filter { get; set; }
    [DisplayFormat(ConvertEmptyStringToNull = false)]
    public string initiation_month { get; set; }
    [DisplayFormat(ConvertEmptyStringToNull = false)]
    public string initiation_year { get; set; }
}

But I absolutely friggin hate the idea of having to create a class for a once off use.

Ive done a lot of research and am really struggling to find a way to change the default behaviour other than above. I really just want to do this:

    [HttpGet]
    public object Search(string batch_number, string filter, string initiation_month, string initiation_year)
    {
    }

Am I missing an easy to way to change this default behaviour or what should I be looking at to impelement my own query string parser that I can apply globally?

Thanks

Update

There seems to be some confusion about my post, sorry if I wasn't clear. I will try to clarify.

I want to pass in just simple primitive types to my HttpGet method as shown in the second code snippet. The problem I have is that empty string parameters will get converted to null.

ie. this url: http://localhost/api/cutting/search?batch_number=&filter=&intiation_month=Jan&initiation_year=2016

will produce the following values in the api:

batch_number = null
filter = null
initiation_month = Jan
initiation_year = 2016

If I change the search function to use the class in the first code snippet, it will work as I want, but Im really trying to avoid using classes for api parameters in the long term.

like image 210
crazyhor77 Avatar asked Mar 12 '16 13:03

crazyhor77


People also ask

Can query parameters be empty?

Yes, it is valid. If one simply want to check if the parameter exists or not, this is one way to do so. Save this answer.

How do you define an empty string?

An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters. A null string is represented by null . It can be described as the absence of a string instance.

Why do we use an empty string?

The empty string is the special case where the sequence has length zero, so there are no symbols in the string. There is only one empty string, because two strings are only different if they have different lengths or a different sequence of symbols.


1 Answers

Ok, I got this working the way I want. I had to adapt some similar code I found for an mvc web api, but made it a lot simpler. Create your custom model binder as below and add it to the globalconfiguration. Hope this helps someone else.

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services
        GlobalConfiguration.Configuration.BindParameter(typeof(string), new EmptyStringModelBinder());

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}"
        );
    }
}

public class EmptyStringModelBinder : System.Web.Http.ModelBinding.IModelBinder
{
    public bool BindModel(HttpActionContext actionContext, System.Web.Http.ModelBinding.ModelBindingContext bindingContext)
    {
        string val = bindingContext.ValueProvider.GetValue(bindingContext.ModelName).AttemptedValue;
        bindingContext.Model = val;

        return true;
    }
}
like image 66
crazyhor77 Avatar answered Oct 05 '22 12:10

crazyhor77