我正在使用參數值調用操作。但它不是爲動作方法參數分配參數值。具有可爲空參數的MVC操作方法
public ActionResult UserDetail(long? userId)
{
}
本地主機/管理/ UserDetail/10 - > 10不傳遞到USERID
但是
本地主機/管理/ UserDetail /用戶id = 10? - > This works
是什麼導致第一個網址無效?任何幫助?
更新:
在Global.asax中嘗試這個仍然沒有工作
routes.MapRoute("ExistSiteUser",
"UserDetail/{userId}",
new
{
controller = "Admin",
action = "UserDetail",
// nothing optional
}
);
routes.MapRoute("NewSiteUser",
"UserDetail",
new
{
controller = "Admin",
action = "UserDetail",
userId = UrlParameter.Optional
}
);
默認情況下,mvc路由可容納1個稱爲id的路由參數。要麼添加路由來包含userId,要麼只是使用id。 – 2013-03-23 05:29:53
@DaveA,你可以給你添加一個接受'userId'的新路線嗎?我試圖在global.asax,但不知道我在那裏犯了什麼錯誤 – Billa 2013-03-23 05:38:16
@Billa請考慮綠色檢查這個答案,如果它服務於你的目的 – 2013-03-23 05:39:26