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

白昼怎懂夜的黑 提交于 2020-01-22 20:00:33

问题


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.


回答1:


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



回答2:


I believe that is by design. If the ModelBinder cannot map the parameter, it reverts to the default type of the parameter.

The same would happen if it was a simple value type like int where it would set that value to 0

Have a look at the following article to see if it can help you

Parameter Binding in ASP.NET Web API



来源:https://stackoverflow.com/questions/35958122/treat-empty-query-string-parameters-as-empty-strings-without-using-a-class-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!