<原>ASP.NET 学习笔记之HTML helper中参数何时会是路由参数,何时又会是query string?

时间:2021-09-24 19:27:31
HTML helper中参数何时会是路由参数,何时又会是query string?
 
@Html.ActionLink("Edit", "Edit", new { idnumber = item.OrderTypeID }) 
 
在形如上述的html helper中,第三个参数是routeValues,如果第三个参数并没有出现在App_Start中的路由文件RouteConfig.cs中(url: "{controller}/{action}/{id}/{actionid}"),则第三个参数会以query string 出现在url中,即http://XXX/ordertypes/Edit?idnumber=1.需要指出的是,routeValue是可以不出现在url的路由定义中的,没有出现过的,一律将以query string 出现在url中。
 
 
@Html.ActionLink("Edit", "Edit", new { id = item.OrderTypeID }) 
 
但若第三个参数出现在url: "{controller}/{action}/{id}/{actionid}"定义中,则会作为路由参数出现在url中,即http://XXX/ordertypes/Edit/1
(默认路由为ordertypes控制器中edit方法,参数id=1)
 
总之,路由参数都会以上述两种形式被写入到url中,传递给controller,但必须保证views中html helper的参数名要与controller中相应的actionMethod中的参数名一致,这样才能正确地将view中数据传递给controller。