Asp.Net Web Api – 发布英国日期格式

我希望我的用户能够以英国格式发布日期到asp.net web api控制器,例如2012年12月1日(2012年12月1日).

根据我的认情况,只接受我们的格式.

我可以在某处更改某些内容,以便英国格式是认格式吗?我尝试在web.config中更改全球化设置,但这没有任何效果.

保罗

解决方法

使用自定义模型绑定器完成此操作,这与MVC3中的模型绑定器略有不同:
public class DateTimeModelBinder : IModelBinder
    {

        public bool BindModel(HttpActionContext actionContext,ModelBindingContext bindingContext)
        {
            var date = bindingContext.ValueProvider.GetValue(bindingContext.ModelName).AttemptedValue;

            if (String.IsNullOrEmpty(date))
                return false;

            bindingContext.ModelState.SetModelValue(bindingContext.ModelName,bindingContext.ValueProvider.GetValue(bindingContext.ModelName));
            try
            {
                bindingContext.Model = DateTime.Parse(date);
                return true;
            }
            catch (Exception)
            {
                bindingContext.ModelState.AddModelError(bindingContext.ModelName,String.Format("\"{0}\" is invalid.",bindingContext.ModelName));
                return false;
            }
        }
    }

在我的Global.asax.cs文件中,添加此行以告诉api将此模型绑定器用于DateTime值:

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

这是我的api控制器中的方法

public IList<LeadsLeadRowviewmodel> Get([ModelBinder]LeadsIndexviewmodel inputModel)

我的LeadsIndexviewmodel类有几个DateTime属性,现在都是有效的UK日期时间.

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....