Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DateTime初始化时发生异常_C#_Asp.net Mvc 4_Datetime - Fatal编程技术网

C# DateTime初始化时发生异常

C# DateTime初始化时发生异常,c#,asp.net-mvc-4,datetime,C#,Asp.net Mvc 4,Datetime,我正在初始化控制器操作参数中的DateTime变量,如下所示 public ActionResult ForFahrzeug(DateTime initDat = default(DateTime), long id = 0, long days = 0, string ExpGnr = "") { //body } 编译时没有错误,但在运行时发生异常 Server Error in '/' App

我正在初始化控制器操作参数中的
DateTime
变量,如下所示

public ActionResult ForFahrzeug(DateTime initDat = default(DateTime), long id = 0, long   
                             days = 0, string ExpGnr = "")
    {
        //body
    }
编译时没有错误,但在运行时发生异常

Server Error in '/' Application.
Encountered an invalid type for a default value.
Description: An unhandled exception occurred during the execution of the current web   
   request. Please review the stack trace for more information about the error and where    it originated in the code.

Exception Details: System.FormatException: Encountered an invalid type for a default value.
堆栈跟踪:

[FormatException: Encountered an invalid type for a default value.]
System.Reflection.MdConstant.GetValue(MetadataImport scope, Int32 token, RuntimeTypeHandle   fieldTypeHandle, Boolean raw) +10764128
System.Reflection.RuntimeParameterInfo.GetDefaultValueInternal(Boolean raw) +413
System.Reflection.RuntimeParameterInfo.GetDefaultValue(Boolean raw) +31
System.Reflection.RuntimeParameterInfo.get_DefaultValue() +6
System.Web.Mvc.ParameterInfoUtil.TryGetDefaultValue(ParameterInfo parameterInfo, Object& value) +64
System.Web.Mvc.ReflectedParameterDescriptor.get_DefaultValue() +59
System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +541
System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +199
System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__1e(AsyncCallback asyncCallback, Object asyncState) +978
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +161
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +92
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag) +53
System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +538
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__17(AsyncCallback asyncCallback, Object asyncState) +83
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +161
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +92
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag, Int32 timeout) +97
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag) +53
System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +531
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +161
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +92
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag, Int32 timeout) +97
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag) +53
System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +447
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +49
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__2(AsyncCallback asyncCallback, Object asyncState) +127
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +161
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +92 
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag, Int32 timeout) +97
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag) +53
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +388
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +88
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +50
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
[格式异常:遇到默认值的无效类型。]
System.Reflection.MdConstant.GetValue(MetadataImport作用域,Int32令牌,RuntimeTypeHandle字段类型句柄,布尔原始)+10764128
System.Reflection.RuntimeParameterInfo.GetDefaultValueInternal(布尔原始)+413
System.Reflection.RuntimeParameterInfo.GetDefaultValue(布尔原始值)+31
System.Reflection.RuntimeParameterInfo.get_DefaultValue()+6
System.Web.Mvc.ParameterInfo.TryGetDefaultValue(ParameterInfo ParameterInfo,对象和值)+64
System.Web.Mvc.ReflectedParameterDescriptor.get_DefaultValue()+59
System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext ControllerContext,ParameterDescriptor ParameterDescriptor)+541
System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext ControllerContext,ActionDescriptor ActionDescriptor)+199
System.Web.Mvc.Async.c__显示类25.b__1e(AsyncCallback AsyncCallback,Object asyncState)+978
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(异步回调,对象状态,Int32超时)+161
System.Web.Mvc.Async.AsyncResultRapper.Begin(AsyncCallback回调,对象状态,BeginInvokeDelegate beginDelegate,EndInvokeDelegate`1 endDelegate,对象标记,Int32超时)+92
System.Web.Mvc.Async.AsyncResultRapper.Begin(AsyncCallback回调,对象状态,BeginInvokeDelegate beginDelegate,EndInvokeDelegate`1 endDelegate,对象标记)+53
System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext ControllerContext,String actionName,AsyncCallback回调,对象状态)+538
System.Web.Mvc.c__displayClassId.b_17(AsyncCallback-AsyncCallback,Object-asyncState)+83
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(异步回调,对象状态,Int32超时)+161
System.Web.Mvc.Async.AsyncResultRapper.Begin(AsyncCallback回调,对象状态,BeginInvokeDelegate beginDelegate,EndInvokeDelegate`1 endDelegate,对象标记,Int32超时)+92
System.Web.Mvc.Async.AsyncResultRapper.Begin(异步回调、对象状态、BeginInvokeDelegate beginDelegate、EndInvokeDelegate endDelegate、对象标记、Int32超时)+97
System.Web.Mvc.Async.AsyncResultRapper.Begin(AsyncCallback回调,对象状态,BeginInvokeDelegate beginDelegate,EndInvokeDelegate endDelegate,对象标记)+53
System.Web.Mvc.Controller.BeginExecuteCore(异步回调,对象状态)+531
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(异步回调,对象状态,Int32超时)+161
System.Web.Mvc.Async.AsyncResultRapper.Begin(AsyncCallback回调,对象状态,BeginInvokeDelegate beginDelegate,EndInvokeDelegate`1 endDelegate,对象标记,Int32超时)+92
System.Web.Mvc.Async.AsyncResultRapper.Begin(异步回调、对象状态、BeginInvokeDelegate beginDelegate、EndInvokeDelegate endDelegate、对象标记、Int32超时)+97
System.Web.Mvc.Async.AsyncResultRapper.Begin(AsyncCallback回调,对象状态,BeginInvokeDelegate beginDelegate,EndInvokeDelegate endDelegate,对象标记)+53
System.Web.Mvc.Controller.BeginExecute(RequestContext RequestContext,AsyncCallback回调,对象状态)+447
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext RequestContext,AsyncCallback回调,对象状态)+49
System.Web.Mvc.c_uuudisplayClass8.b_uuu2(异步回调,异步回调,对象异步状态)+127
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(异步回调,对象状态,Int32超时)+161
System.Web.Mvc.Async.AsyncResultRapper.Begin(AsyncCallback回调,对象状态,BeginInvokeDelegate beginDelegate,EndInvokeDelegate`1 endDelegate,对象标记,Int32超时)+92
System.Web.Mvc.Async.AsyncResultRapper.Begin(异步回调、对象状态、BeginInvokeDelegate beginDelegate、EndInvokeDelegate endDelegate、对象标记、Int32超时)+97
System.Web.Mvc.Async.AsyncResultRapper.Begin(AsyncCallback回调,对象状态,BeginInvokeDelegate beginDelegate,EndInvokeDelegate endDelegate,对象标记)+53
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext,异步回调,对象状态)+388
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext HttpContext,异步回调,对象状态)+88
System.Web.Mvc.MvcHandler.System.Web.IHTTPassynchandler.BeginProcessRequest(HttpContext上下文,AsyncCallback cb,Object extraData)+50
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+301
System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值&同步完成)+155

此处报告此行为:

似乎是一只虫子

您可以选择这样做:

 public ActionResult ForFahrzeug(DateTime? initDat = null, ...
 {
 }

此处报告了此行为:

似乎是一只虫子

您可以选择这样做:

 public ActionResult ForFahrzeug(DateTime? initDat = null, ...
 {
 }

堆栈跟踪已更新。堆栈跟踪已更新。或者
DateTime initDat=DateTime。现在
@L-Three由于此解决方案,还有一个问题需要解决。例如,我有两个变量sof
DateTime
like
DateTime?initDate=null,DateTime?finalDate=null
我想找出差异。那现在就不行了。似乎这两个变量的类型不再是
DateTime
。我发现差异
int days=(finalDate-initDate)在使用它们之前,必须使用HasValue检查是否存在null。如果(finalDate.HasValue){…}@L-3像这样
if(initDate!=null&&finalDate!=null){days=(int)(finalDate-initDate).TotalDays;}
FYI:Microsoft解决了上面链接的连接问题,称为“无法修复”,链接不再工作。MS在2012年4月6日的最后一句评论是:“不幸的是,我们现在无法解决这个问题