2013-05-07 50 views
1

在ASP MVC3項目一個電話: -電話Url.RouteUrl不工作的部署網站

<h2><a href="@Url.RouteUrl("_ScientificPrograms", new { idProgramaSeleccionado = @programa.Id, nombrePrograma = @programa.NombreProgramaUrl(), IdiomaSeleccion = idioma })">@programa.NombrePrograma(@idioma)</a></h2> 

對應於的Global.asax.cs中文件的註冊路線: -

routes.MapRoute(
      "_ScientificPrograms", // Route name 
      "{IdiomaSeleccion}/research/scientific-programmes/{idProgramaSeleccionado}/{nombrePrograma}/{idGrupoSeleccionado}/{nombreGrupo}", 
      new { controller = "Research", action = "ScientificProgrammes", IdiomaSeleccion = UrlParameter.Optional, idProgramaSeleccionado = UrlParameter.Optional, nombreGrupo = UrlParameter.Optional, idGrupoSeleccionado = UrlParameter.Optional, nombrePrograma = UrlParameter.Optional } 
     ); 

工作在我的本地機器產生的URL喜歡精緻的運行: -

http://localhost/es/research/scientific-programmes/1/molecular-oncology 

當網站被部署,但是,同樣只調用PR oduces

http://deployedsite/es/research/ 

雖然進入

http://deployedsite/es/research/scientific-programmes/1/molecular-oncology 

不正確解析頁面。

相同的行爲觀察我自己的測試主機和我的客戶QA服務器上都。其他路由調用工作正常,但看起來這是一些特別的。

回答

0

事實證明,將最新的.NET服務包應用到服務器可以解決無代碼更改的問題。 It-Works-On-My-Machine的行爲原來是吸菸槍。

1

您可能已經解決了這個或移動,但我會在這裏把這個信息的情況下,其他人都會碰到你的問題,因爲他們是來自同一個MVC3錯誤,導致此問題的痛苦。

事實證明,連續UrlParameter.Optional參數引起MVC3一個小bug,按照this blog entry from Phil Haack

當您有一個具有兩個連續可選URL參數的路由並且您嘗試使用路由來生成URL時,該錯誤就會顯現。傳入的請求匹配行爲不變,並繼續正常工作。

解決方法很簡單。要解決此問題,請通過刪除月和日的默認值將現有路由更改爲不具有任何可選參數。此路線現在處理指定月份和日期的第一個URL。 然後,我們爲其他兩種情況添加一條新路線,但此路線只有一個可選的月份參數。

就我而言,我有這樣的路線:

routes.MapRoute(
      "HouseholdComments", 
      "Comments/Household/{id}/{saQid}/{clId}", 
      new { controller = "Comments", action = "Household", id = UrlParameter.Optional, saQid = UrlParameter.Optional, clId = UrlParameter.Optional } 
     ); 

我試圖用Url.RouteUrl這樣的:

var questionHref = '@Url.RouteUrl("HouseholdComments", new {[email protected]_SEQ_NO})' 

認爲它應該只是只用1個參數調用路由。但它沒有,它返回null。如上文菲爾的文章,我有後我HouseholdComments路線添加一個單獨的路由這種情況

routes.MapRoute(
      "HouseholdCommentsBase", 
      "Comments/Household/{id}", 
      new {controller = "Comments", action="Household", id=UrlParameter.Optional} 
     ); 

然後我能夠使用Url.RouteUrl這條路線正確地得到我所需要的鏈接。

我猜你應該只需要添加另一條路線來處理下,如果只只使用3個參數在Url.RouteUrl的情況。