public
void
SaveTask(Guid id,
string
title,
bool
?
isPublic
)
准备让它接收"CreateTask"和"EditTask"创建的表单:
<
form
action
="SaveTask"
method
="post"
>
< dl >
< dt > 标题 </ dt >
< dd >< input type ="text" value ="" size ="20" name ="title" id ="title" /></ dd >
< dt > 公开 </ dt >
< dd >< input type ="checkbox" value ="true" name ="isPublic" /> true </ dd >
</ dl >
< input type ="hidden" value ="00000000-0000-0000-0000-000000000000" name ="id" id ="id" />
< input type ="submit" value ="创建任务" name ="submit" id ="submit" />
</ form >
但是每次提交表单,都会出现异常:
< dl >
< dt > 标题 </ dt >
< dd >< input type ="text" value ="" size ="20" name ="title" id ="title" /></ dd >
< dt > 公开 </ dt >
< dd >< input type ="checkbox" value ="true" name ="isPublic" /> true </ dd >
</ dl >
< input type ="hidden" value ="00000000-0000-0000-0000-000000000000" name ="id" id ="id" />
< input type ="submit" value ="创建任务" name ="submit" id ="submit" />
</ form >
[FormatException: GUID 应包含带
4
个短划线的
32
位数(xxxxxxxx
-
xxxx
-
xxxx
-
xxxx
-
xxxxxxxxxxxx)。]
System.Guid..ctor(String g) + 2284
System.ComponentModel.GuidConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) + 103
System.ComponentModel.TypeConverter.ConvertFrom(Object value) + 54
System.Web.Mvc.Controller.ConvertParameterType(Object value, Type destinationType, String parameterName, String actionName) + 161
[InvalidOperationException: Cannot convert parameter 'id' in action 'SaveTask' with value '' to type 'System.Guid' .]
System.Web.Mvc.Controller.ConvertParameterType(Object value, Type destinationType, String parameterName, String actionName) + 301
System.Web.Mvc.Controller.InvokeActionMethod(MethodInfo methodInfo, RouteValueDictionary values) + 760
[InvalidOperationException: A value is required for parameter ' id ' in action ' SaveTask ' . The parameter either has no value or its value could not be converted. To make a parameter optional its type should either be a reference type or a Nullable type.]
System.Web.Mvc.Controller.InvokeActionMethod(MethodInfo methodInfo, RouteValueDictionary values) + 902
System.Web.Mvc.Controller.InvokeAction(String actionName, RouteValueDictionary values) + 263
System.Web.Mvc.Controller.Execute(ControllerContext controllerContext) + 161
System.Web.Mvc.Controller.System.Web.Mvc.IController.Execute(ControllerContext controllerContext) + 4
System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) + 212
System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) + 36
System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) + 4
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() + 358
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean & completedSynchronously) + 64
System.Guid..ctor(String g) + 2284
System.ComponentModel.GuidConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) + 103
System.ComponentModel.TypeConverter.ConvertFrom(Object value) + 54
System.Web.Mvc.Controller.ConvertParameterType(Object value, Type destinationType, String parameterName, String actionName) + 161
[InvalidOperationException: Cannot convert parameter 'id' in action 'SaveTask' with value '' to type 'System.Guid' .]
System.Web.Mvc.Controller.ConvertParameterType(Object value, Type destinationType, String parameterName, String actionName) + 301
System.Web.Mvc.Controller.InvokeActionMethod(MethodInfo methodInfo, RouteValueDictionary values) + 760
[InvalidOperationException: A value is required for parameter ' id ' in action ' SaveTask ' . The parameter either has no value or its value could not be converted. To make a parameter optional its type should either be a reference type or a Nullable type.]
System.Web.Mvc.Controller.InvokeActionMethod(MethodInfo methodInfo, RouteValueDictionary values) + 902
System.Web.Mvc.Controller.InvokeAction(String actionName, RouteValueDictionary values) + 263
System.Web.Mvc.Controller.Execute(ControllerContext controllerContext) + 161
System.Web.Mvc.Controller.System.Web.Mvc.IController.Execute(ControllerContext controllerContext) + 4
System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) + 212
System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) + 36
System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) + 4
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() + 358
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean & completedSynchronously) + 64
注意加下划线的部分:不是明明有回传的值" 00000000-0000-0000-0000-000000000000"吗,怎么变成了空字符串了呢?谁吃了我的参数值?
经过两个小时艰苦卓绝的调查取证(其中包含了到处搜索Asp.net MVC preview 2源码,以及试图使用Reflector重建源项目未遂),最后灵机一动,把"id"改成"taskId",于是就一切正常了.
结论很显然,"id"这个参数名在默认情况下是受到特殊对待的,只能在URL里附带,不能post..