瘋狂的猜測:
可能你的路由是在默認路由後註冊的。把它作爲你的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")
}
);
的ActionLink的工作得很好,當默認路由前加入上述路線。但是,默認現在去/首頁/詳細信息 – Danny