话说我有一个action:
public void SaveTask(Guid id, string title, bool? isPublic)

准备让它接收"CreateTask"和"EditTask"创建的表单:
Asp.net MVC, 谁吃了我的"id"参数值?    <form action="SaveTask" method="post">
Asp.net MVC, 谁吃了我的"id"参数值?        
<dl>
Asp.net MVC, 谁吃了我的"id"参数值?            
<dt>标题</dt>
Asp.net MVC, 谁吃了我的"id"参数值?            
<dd><input type="text"  value=""  size="20"  name="title"  id="title"  /></dd>
Asp.net MVC, 谁吃了我的"id"参数值?           
<dt>公开</dt>
Asp.net MVC, 谁吃了我的"id"参数值?Asp.net MVC, 谁吃了我的"id"参数值?         
<dd><input type="checkbox"  value="true"  name="isPublic"  />&nbsp;true</dd>
Asp.net MVC, 谁吃了我的"id"参数值?        
</dl>
Asp.net MVC, 谁吃了我的"id"参数值?      
<input type="hidden"  value="00000000-0000-0000-0000-000000000000"  name="id"  id="id"  />
Asp.net MVC, 谁吃了我的"id"参数值?        
<input type="submit"  value="创建任务"  name="submit"  id="submit"  />
Asp.net MVC, 谁吃了我的"id"参数值?    
</form>Asp.net MVC, 谁吃了我的"id"参数值?
但是每次提交表单,都会出现异常:
Asp.net MVC, 谁吃了我的"id"参数值?[FormatException: GUID 应包含带 4 个短划线的 32 位数(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)。]
Asp.net MVC, 谁吃了我的"id"参数值?   System.Guid..ctor(String g) 
+2284
Asp.net MVC, 谁吃了我的"id"参数值?   System.ComponentModel.GuidConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) 
+103
Asp.net MVC, 谁吃了我的"id"参数值?   System.ComponentModel.TypeConverter.ConvertFrom(Object value) 
+54
Asp.net MVC, 谁吃了我的"id"参数值?   System.Web.Mvc.Controller.ConvertParameterType(Object value, Type destinationType, String parameterName, String actionName) 
+161
Asp.net MVC, 谁吃了我的"id"参数值?
Asp.net MVC, 谁吃了我的"id"参数值?[InvalidOperationException: Cannot convert parameter 
'id' in action 'SaveTask' with value '' to type 'System.Guid'.]
Asp.net MVC, 谁吃了我的"id"参数值?   System.Web.Mvc.Controller.ConvertParameterType(Object value, Type destinationType, String parameterName, String actionName) 
+301
Asp.net MVC, 谁吃了我的"id"参数值?   System.Web.Mvc.Controller.InvokeActionMethod(MethodInfo methodInfo, RouteValueDictionary values) 
+760
Asp.net MVC, 谁吃了我的"id"参数值?
Asp.net MVC, 谁吃了我的"id"参数值?[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.]
Asp.net MVC, 谁吃了我的"id"参数值?   System.Web.Mvc.Controller.InvokeActionMethod(MethodInfo methodInfo, RouteValueDictionary values) 
+902
Asp.net MVC, 谁吃了我的"id"参数值?   System.Web.Mvc.Controller.InvokeAction(String actionName, RouteValueDictionary values) 
+263
Asp.net MVC, 谁吃了我的"id"参数值?   System.Web.Mvc.Controller.Execute(ControllerContext controllerContext) 
+161
Asp.net MVC, 谁吃了我的"id"参数值?   System.Web.Mvc.Controller.System.Web.Mvc.IController.Execute(ControllerContext controllerContext) 
+4
Asp.net MVC, 谁吃了我的"id"参数值?   System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) 
+212
Asp.net MVC, 谁吃了我的"id"参数值?   System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) 
+36
Asp.net MVC, 谁吃了我的"id"参数值?   System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) 
+4
Asp.net MVC, 谁吃了我的"id"参数值?   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 
+358
Asp.net MVC, 谁吃了我的"id"参数值?   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean
& completedSynchronously) +64
Asp.net MVC, 谁吃了我的"id"参数值?

注意加下划线的部分:不是明明有回传的值"00000000-0000-0000-0000-000000000000"吗,怎么变成了空字符串了呢?谁吃了我的参数值?
  经过两个小时艰苦卓绝的调查取证(其中包含了到处搜索Asp.net MVC preview 2源码,以及试图使用Reflector重建源项目未遂),最后灵机一动,把"id"改成"taskId",于是就一切正常了.

  结论很显然,"id"这个参数名在默认情况下是受到特殊对待的,只能在URL里附带,不能post..

相关文章: