我是ASP.Net MVC的新手,現在遇到了問題。這里是。routes.MapRoute( "SearchResults",// Route name "{controller}/{action}/{category}/{manufacturer}/{attribute}", new { controller = "Home", action = "CategoryProducts", category = UrlParameter.Optional, manufacturer = UrlParameter.Optional, attribute = UrlParameter.Optional });這是我的控制器方法。public ActionResult CategoryProducts(string category, string manufacturer, string attribute) { string[] categoryParameter = category.Split('_'); . . . return View();}當我點擊url時,類別參數始終為nullhttp://localhost:50877/Home/CategoryProducts/c_50_ShowcasesDisplays我得到這個錯誤Object reference not set to an instance of an object我該如何解決這個問題。我需要從細分中提取ID并使用它。同樣,我也需要處理制造商和屬性字符串。還有一件事我如何使我的函數獲得至少一個參數而不受順序限制?我的意思是我想做類似的功能,以便可以處理類別或制造商或屬性或類別+制造商以及所有組合/
2 回答

紫衣仙女
TA貢獻1839條經驗 獲得超15個贊
只有最后一個參數可以標記為UrlParameter.Optional
(否則路由引擎無法匹配哪個參數)。為了使當前的實現工作有效,您需要使用http://localhost:50877/Home/CategoryProducts?category=c_50_ShowcasesDisplays
- 2 回答
- 0 關注
- 541 瀏覽
添加回答
舉報
0/150
提交
取消