Asp.Net的Web API - ModelBinders [英] Asp.Net Web Api - ModelBinders

查看:229
本文介绍了Asp.Net的Web API - ModelBinders的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用某种类型的自定义模型绑定器在英国的格式始终将传入的日期,我已经建立了自定义模型粘结剂像这样注册的:

I need to use a custom modelbinder of some kind to always treat incoming dates in UK format, i have setup a custom model binder and registered like so:

GlobalConfiguration.Configuration.BindParameter(typeof(DateTime), new DateTimeModelBinder());

这似乎只为查询字符串参数工作,如果我对我的参数,像这样指定[ModelBinder的]才工作,有我的方式,使所有操作用我的模型绑定:

This only seems to work for querystring parameters, and only works if i specify [ModelBinder] on my parameter like so, is there i way to make all actions use my model binder:

public IList<LeadsLeadRowViewModel> Get([ModelBinder]LeadsIndexViewModel inputModel)

另外,我怎么可以让我贴的形式到我的API控制器使用我的模型绑定?

Also, how can i get my posted form to my Api controller to use my model binder?

推荐答案

我觉得你不需要一个模型粘合剂。你的做法是不正确。对于日期正确的方法是使用客户端库的全球化像全球化库解析任何语言格式的日期并将其转换成JavaScript日期对象。然后,你可以序列化datascript数据结构与浏览器JSON.stringify JSON,这应该工作。
这是更好地一直使用标准格式的日期,使用格式,而不是比模型粘合剂。可用格式程序还处理时区正确,如果使用的一种的你的C#的DateTime对象的参数来指定,如果日期时间为本地时间或UTC时间pssed前$ P $。

I think you dont need a model binder. Your approach is incorrect. The right approach for dates is using a client side globalization library like the globalize library to parse dates formatted in any language and transform them into JavaScript date objects. Then you can serialize your datascript data structures in json with the browser JSON.stringify, and this should work. It is better to use always standard formats for dates and to use a formatter instead than a model binder. Available formatters handle also TimeZones correctly, if you use the kind parameter of your C# DateTime objects to specify if the date time is expressed in local time or in UTC time.

这篇关于Asp.Net的Web API - ModelBinders的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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