Bind a JSON date value¶
Bind a request value to a DateTime, tolerating missing or unparseable input. Trigger: MVC model binding for a bound DateTime when the binder is registered.
| Field | Value |
|---|---|
| Operation ID | OP-18 |
| Trigger | MVC model binding for a bound DateTime when the binder is registered |
| Inputs | bindingContext.ValueProvider value for ModelName |
| Outputs | DateTime, or the raw null value |
| Primary components | JsonDateTimeModelBinder.BindModel(ControllerContext, ModelBindingContext) |
| Entry point | src/Web.Mvc/ModelBinders/JsonDateTimeModelBinder.cs |
Steps¶
- When no value is found, return
new DateTime()(0001-01-01) (BR-39). - When
value.RawValueis null, returnvalue.RawValue(null). - When the attempted value starts with
/, treat it as the ASP.NET JSON date form/Date(ms)/: take the substring from index 6, strip the trailing)/, add that many milliseconds to the Unix epoch1970-01-01, and convert withToLocalTime()(BR-40). - Otherwise call
DateTime.TryParse; on failure the out value remainsdefault(DateTime)and is returned, so a parse failure never raises a binding error (BR-41).
Process flow¶
The following diagram visualizes this operation's control flow.
flowchart TD
Start([Model binding])
NoVal{Value found?}
Default([Return DateTime default])
Null{RawValue null?}
ReturnNull([Return null])
Slash{Starts with /?}
JsonDate["Parse /Date(ms)/ epoch form"]
TryParse[DateTime.TryParse]
EndOk([Return DateTime])
Start --> NoVal
NoVal -->|No| Default
NoVal -->|Yes| Null
Null -->|Yes| ReturnNull
Null -->|No| Slash
Slash -->|Yes| JsonDate
Slash -->|No| TryParse
JsonDate --> EndOk
TryParse --> EndOk
Failure and edge-case behavior¶
- No unexpected failure paths beyond argument-guard exceptions and delegated dependency behavior were identified for this operation.
Related documentation¶
Sources¶
src/Web.Mvc/ModelBinders/JsonDateTimeModelBinder.cs