2016-09-21 45 views
0

我想配置自定義URL在RouteConfig.cs文件作爲默認的:當我寫的以下內容檢索查詢字符串,並迫使自定義網址到頁面

public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

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

     routes.MapRoute( 
      name: "Default2", 
      url: "{controller}/{action}/id/{id}", //Custom url route 
      defaults: new { controller = "Product", action = "AddImages", id = UrlParameter.Optional } 
     ); 
    } 

以上工作正常網址:1路由

http://localhost:1234/Product/AddImages/id/1008 

同樣,以下也適用:第二路由

http://localhost:1234/Product/AddImages/1008 

但我想迫使網址顯示或寫在地址欄中的URL作爲第一個面向特定頁面「AddImages」是這樣的:http://localhost:1234/Product/AddImages/id/1008

有沒有辦法做到這一點我的意思是保持默認路由和特定頁面的第一個路由選項?

同樣,我試圖從第一個網址檢索查詢字符串如下:

int id = Convert.ToInt32(Url.RequestContext.RouteData.Values["id"]); //This works for the second routing option 

它說 - 輸入字符串的不正確的格式。我想,這是默認的路由選擇,因爲它的配置爲:

url: "{controller}/{action}/{id}" 

再次,是它可以從以下網址獲得查詢字符串值或配置任何東西:

http://localhost:1234/Product/AddImages/id/1008 
+1

您的航線順序錯誤 - Default2必須在第一位(默認值之前) –

+0

@Stephen Muecke我可以通過對路線進行排序來解決問題。感謝你和無腦編碼器的想法。 –

回答

1

路由MVC的表中,首先定義的路由具有最高的優先級。所以你需要的是確保你的具體路線比默認路線更高。只是早些時候定義它。就是這樣 - 正如評論中提到的那樣。

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

    routes.MapRoute( 
     name: "Default2", 
     url: "{controller}/{action}/id/{id}", //Custom url route 
     defaults: new { controller = "Product", action = "AddImages", id = UrlParameter.Optional } 
    ); 

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

如果你想只有第一個,然後嘗試刪除通用一個

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

    routes.MapRoute( 
     name: "Default2", 
     url: "{controller}/{action}/id/{id}", //Custom url route 
     defaults: new { controller = "Product", action = "AddImages", id = UrlParameter.Optional } 
    ); 
} 

如果你希望它僅用於控制器僅試試這個 -

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

    routes.MapRoute( 
     name: "Default2", 
     url: "Product/{action}/id/{id}", //Custom url route 
     defaults: new { action = "AddImages", id = UrlParameter.Optional }, 
     constraints: new { controller = "Product" } 
    ); 

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

有一個更好獲取網址參數的方法。取而代之的

int id = Convert.ToInt32(Url.RequestContext.RouteData.Values["id"]); // 

試試這個 -

int id = Convert.ToInt32(Request.Params["id"]); // 

但是,如果你正確地定義你的操作方法,你甚至都不需要從請求讀取它。它會自動映射。

+0

無腦編碼器!感謝您的解決方案。我是 無法從查詢字符串獲取值,如 this:/ AddImages/id/2。但它適用於 this:/ AddImages/2。有沒有辦法獲得 的自定義url值?我試過你的解決方案,但 沒有工作我的意思是沒有得到價值。順便說一下,約束不會強制控制器訪問特定的url。有什麼配置嗎? –

+0

@ AT-2016我不記得,自從我使用顯式路由以來,我一直使用屬性路由,我會測試並更新答案。 –

+0

感謝您的回覆。我會等待。基本上我願意從這個路由獲取url或QueryString的值:/ AddImages/id/2(Custom routing)。我不確定這是否可能。如果是這樣,那麼我可以處理其餘的事情。 –