将空查询字符串参数视为空字符串,而不使用参数类 [英] Treat empty query string parameters as empty strings, without using a class for parameters

查看:129
本文介绍了将空查询字符串参数视为空字符串,而不使用参数类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将多个参数传递给httpget Web api函数.我遇到的关键问题是将空查询字符串参数转换为null.

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?

谢谢

更新

我的帖子似乎有些混乱,如果不清楚,抱歉.我会尽力澄清.

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

我想将简单的原始类型传递给我的HttpGet方法,如第二个代码片段所示.我的问题是空字符串参数将转换为null.

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

将在api中产生以下值:

will produce the following values in the api:

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

如果我将搜索功能更改为在第一个代码段中使用该类,它将按我的意愿工作,但是我实际上是在长期内避免使用类作为api参数.

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.

推荐答案

好,我已经按照我想要的方式工作了.我不得不修改我为mvc网络api找到的一些类似代码,但使其变得更加简单.如下创建您的自定义模型联编程序并将其添加到globalconfiguration.希望这对其他人有帮助.

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

这篇关于将空查询字符串参数视为空字符串,而不使用参数类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆