0

我有兩條路線在我的應用程序設置:ASP.NET路由MVC4問題 - 無法生成URL路由

routeCollection.MapRoute("CustomerActivity", 
         "Customer/{id}", 
         new { controller = "CustomerActivity", action = "DisplayDetails" }, 
         new { id = @"^\d$" }); 
routeCollection.MapRoute("CustomerSearch", 
         "Customer/{*pathInfo}", 
         new 
          { 
          controller = "CustomerSearch", 
          action = "DisplaySearch" 
          }); 

傳入URL是正確地路由到正確的控制器/動作的配對。然而,在視圖我需要生成一個錨從而以查看客戶的詳細信息:

@Html.ActionLink(Model.Name, "DisplayDetails", "CustomerActivity", new { id = Model.Id }, null) 

的問題,這是它實際上並沒有挑選任何路由起來的;我相信這是因爲CustomerActivity路線的限制。

@Html.RouteLink(Model.Name, "CustomerActivity", new { id = Model.Id }) 

我不能拿出上CustomerActivity約束,因爲這是一切停止落入這條路線:

我也使用RouteLink沒有多少運氣嘗試。

添加CustomerActivity的副本沒有約束到最後似乎來解決這個問題,但我不到深刻的印象:

routeCollection.MapRoute("CustomerActivity", 
         "Customer/{id}", 
         new { controller = "CustomerActivity", action = "DisplayDetails" }, 
         new { id = @"^\d$" }); 
routeCollection.MapRoute("CustomerSearch", 
         "Customer/{*pathInfo}", 
         new 
          { 
          controller = "CustomerSearch", 
          action = "DisplaySearch" 
          }); 
routeCollection.MapRoute("CustomerActivityUrlCreation", 
         "Customer/{id}", 
         new { controller = "CustomerActivity", action = "DisplayDetails" }); 

我能想到這樣做的唯一的其他東西是從根本上區分Url和擺脫對CustomerActivity的約束,但我不想這樣做。有沒有人有任何其他建議如何解決這個問題?

回答

1

這應該工作。我懷疑你的Model.Id大於9,並因此失去了約束,只允許一個數字。因此,試圖通過允許多個數字調整約束:

routes.MapRoute(
    "CustomerActivity", 
    "Customer/{id}", 
    new { controller = "CustomerActivity", action = "DisplayDetails" }, 
    new { id = @"^\d+$" } 
); 
+0

我希望你是正確的,我會嘗試一下上解決了這一問題後:) – Steve

+0

現貨!爲此非常感謝! – Steve

0
Try this...It will work 

routeCollection.MapRoute("CustomerActivity", 
         "{controller}/{action}/{id}", 
         new { controller = "CustomerActivity", action = "DisplayDetails" }, 
         new { id = @"^\d$" }); 

routeCollection.MapRoute("CustomerSearch", 
         "CustomerSearch/DisplaySearch/{*pathInfo}", 
         new 
          { 
          controller = "CustomerSearch", 
          action = "DisplaySearch" 
          }); 
+0

這個答案有幾個問題:1)第一條路線比第二條路線更通用,因此大多數情況下都會使用; 2)第一條路線仍然存在問題,即預計該ID僅爲一位; 3)它不維護我正在使用的/ Customer/url。 – Steve

+0

首先,你沒有路由中的默認路由......當你創建你的路由時,你必須通過像 - {controller}/{action}/{id}這樣的URL,而你只傳遞「Customer /{id}"./你對/ Customer/url有什麼意義。如果您看到您通過DisplayDetails操作向CutomerActivity cotroller發出的呼叫。所以你需要更多的細節在哪裏打電話。如果不清楚,請讓我知道你想要什麼,因爲這是從我身邊進行測試 – Pushpendra