2013-08-23 16 views
1

我有2種途徑在路線

routes.MapRoute(
     name: "Default", 
     url: "{controller}/{action}/{id}", 
     defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
    ); 

    routes.MapRoute(
     name: "Plugin", 
     url: "{pluginName}/{controller}/{action}/{id}", 
     defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
    ); 

設置自定義參數我想用Html.Action幫手,並設置我的第二條路線的「pluginName」參數。

我嘗試使用下面的代碼

@Html.Action("Index","Person",new RouteValueDictionary { { "pluginName", "myPlugin" } }); 

,並得到像

http://mydomain/myplugin/Person/index 

鏈接,但我已經越來越

http://mydomain/Person/index?pluginName="myPlugin" 

我怎樣才能得到第一個鏈接模式?

回答

2

首先註冊您的更具體的路線。路由引擎按您註冊的順序評估路由。所以,如果你有一個相當普通的路由(在你做的時候),路由引擎會使用它並將其他值作爲QueryString參數(你看到的)附加。

試試這個:

routes.MapRoute(
    name: "Plugin", 
    url: "{pluginName}/{controller}/{action}/{id}", 
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
); 

routes.MapRoute(
    name: "Default", 
    url: "{controller}/{action}/{id}", 
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
); 
+0

謝謝你,它的工作原理 – Polaris

+0

優秀 - 高興能幫上忙。不要忘記標記答案,以便幫助他人。謝謝! –