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

Viewed 5734

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.

2 Answers
Related