1
我已完成在我的routeConfig文件中的路由,如下所示。參數字典在參數'id'中包含一個空條目,當在路由中進行更改時
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Search",
url: "{controller}/{action}/{department}/{name}",
defaults: new { controller = "Search", action = "Result",name =UrlParameter.Optional}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
我只想名字參數應該是可選的,所以寫
name =UrlParameter.Optional
但現在當我點擊這個鏈接
@Html.ActionLink("Edit", "EditEmployee", new { id = item.Id })
上它給我的異常使這個這個「The parameters dictionary contains a null entry for parameter 'id'
「
案例1: 但是當我這樣做時,
defaults: new { controller = "Search", action = "Result"}
刪除名稱可選,它工作正常。
的情況下2: 當我這樣做
defaults: new { controller = "Search", action = "Result", department =UrlParameter.Optional, name=UrlParameter.Optional}
使得無論參數可選,也能正常工作,但編輯聯繫URL生成是遵循
http://localhost:12190/Home/EditEmployee?id=1
,但我想幹淨URL像這 http://localhost:12190/Home/EditEmployee/1
。
那麼如何獲得這兩個,只有名稱參數可選,也是一個乾淨的URL。