C# 更改从URL参数ASP.NET检索的默认日期时间格式

C# 更改从URL参数ASP.NET检索的默认日期时间格式,c#,asp.net,.net,C#,Asp.net,.net,我有网址: http://localhost/Page?id=&created_date=27-09-2017 上述创建日期的日期格式为“dd-MM-yyyy” 同时,我的控制器: public ActionResult Page(string id = null, DateTime? created_date = null) { ... } 嗯,因为URL日期格式是“dd-MM-yyyy”,所以我无法在我的控制器中检索created\u-date(仍然为空)的值。但是,如果我将UR

我有网址:

http://localhost/Page?id=&created_date=27-09-2017
上述创建日期的日期格式为“dd-MM-yyyy”

同时,我的控制器:

public ActionResult Page(string id = null, DateTime? created_date = null)
{
...
}
嗯,因为URL日期格式是“dd-MM-yyyy”,所以我无法在我的控制器中检索
created\u-date
(仍然为空)的值。但是,如果我将URL日期格式更改为“MM dd yyyy”,则
创建日期
已成功填写(2017年9月27日)。例如:

http://localhost/Page?id=&created_date=09-27-2017
如何将此机制全局更改为“dd-MM-yyyy”?因为像这样的URL模式太多了。我不想手动更改URL日期格式

---更新(添加自定义模型活页夹)---

这里是我的
Global.asax.cs

protected void Application_Start()
{
     var binder = new DateTimeModelBinder("dd-MM-yyyy");
     ModelBinders.Binders.Add(typeof(DateTime), binder);
     ModelBinders.Binders.Add(typeof(DateTime?), binder);
     AreaRegistration.RegisterAllAreas();
     GlobalConfiguration.Configure(WebApiConfig.Register);
     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
     RouteConfig.RegisterRoutes(RouteTable.Routes);
     BundleConfig.RegisterBundles(BundleTable.Bundles);
}
DateTimeModelBinder
class:

public class DateTimeModelBinder : DefaultModelBinder
{
     private string _customFormat;

     public DateTimeModelBinder(string customFormat)
     {
         _customFormat = customFormat;
     }

     public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
     {
         var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
         return DateTime.ParseExact(value.AttemptedValue, _customFormat, CultureInfo.InvariantCulture);
     }
}

提前感谢

是否有帮助?编写自定义日期时间活页夹对您有用,请参考Thank@mjwills,但在将活页夹添加到global后仍然不起作用。Asaxi如果您在
BindModel
中设置断点,它会被命中吗?不会,启动应用程序或加载页面@mjwillsDoes或help时未命中?编写自定义日期时间绑定器对您有用,请参考@mjwills,但在将绑定器添加到全局后仍不工作。如果您在
BindModel
中设置断点,是否命中?不,启动应用程序或加载页面@mjwills时未命中