2011-09-21 90 views
10

我已經定義了這條路線:ActionLink在URL中顯示參數而不是查詢字符串?

routes.MapRoute(
        "Details", // Route name 
        "{home}/{details}/{id}/{name}", // URL with parameters 
        new 
        { 
         controller = "Home", 
         action = "Details", 
         id = UrlParameter.Optional, 
         name = UrlParameter.Optional 
        } // Parameter defaults 
       ); 

的ActionLink的:

@Html.ActionLink("Show Details", "Details", "MyController", new { id = 1, name ="a" }) 

的ActionLink的結果/Home/Details/1?name=a我以後/Home/List/1/a

回答

12

你的路由定義應該是這樣的:

routes.MapRoute(
    "Details", // Route name 
    "{controller}/{action}/{id}/{name}", // URL with parameters 
    new 
    { 
     controller = "Home", 
     action = "Details", 
     id = UrlParameter.Optional, 
     name = UrlParameter.Optional 
    } // Parameter defaults 
); 

另外你應該使用proper overload

@Html.ActionLink(
    "Show Details",    // linkText 
    "Details",     // action 
    "MyController",    // controller 
    new { id = 1, name = "a" }, // routeValues 
    null      // htmlAttributes 
) 

通知的null底。

+1

的ActionLink的工作得很好,當默認路由前加入上述路線。但是,默認現在去/首頁/詳細信息 – Danny

1

瘋狂的猜測:

可能你的路由是在默認路由後註冊的。把它作爲你的global.asax內的第一條路線,那麼它將起作用。

象下面這樣:

public static void RegisterRoutes(RouteCollection routes) { 

     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

     routes.MapRoute(
          "Details", // Route name 
          //Put action instead of details 
          "{home}/{action}/{id}/{name}", // URL with parameters 
          new 
          { 
           controller = "Home", 
           action = "Details", 
           id = UrlParameter.Optional, 
           name = UrlParameter.Optional 
          } // Parameter defaults 
         ); 

     routes.MapRoute(
      "Default", // Route name 
      "{controller}/{action}/{id}", // URL with parameters 
      new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults 
     ); 

    } 

UPDATE

@Simon是正確的,但如果你願意,你可以用另外一種方式。

爲了讓路線僅適用於一個操作方法,請使用以下代碼。

創建一個約束條件如下:

public class EqualConstraint : IRouteConstraint { 

    private string _match = String.Empty; 

    public EqualConstraint(string match) { 

     _match = match; 
    } 

    public bool Match(System.Web.HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { 

     return string.Equals(values[parameterName].ToString(), _match); 
    } 
} 

,然後更改像下面路線:

routes.MapRoute(
         "Details", // Route name 
         //Put action instead of details 
         "{home}/{action}/{id}/{name}", // URL with parameters 
         new 
         { 
          controller = "Home", 
          action = "Details", 
          id = UrlParameter.Optional, 
          name = UrlParameter.Optional 
         }, // Parameter defaults 
         new { 
          controller = new EqualConstraint("Home"), 
          action = new EqualConstraint("Details") 
         } 
        ); 
+0

看到我對達林的評論 – Danny

+0

@丹尼你是什麼意思的「默認現在去/首頁/詳細信息」? – tugberk

+0

當應用程序啓動到/ Home/Details,而不是默認路由/ Home/Index – Danny

相關問題