野生の推測:
おそらくあなたのルートがデフォルトルートの後に登録されました。あなたの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は正しいですが、あなたがしたい場合は、別の方法を使用することができます。
ルートが1つのアクションメソッドに対してのみ機能するようにするには、次のコードを使用します。
次のように制約を作成します。その後、
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のはうまく働きました。しかし、デフォルトは現在/ Home/Detailsに移動します – Danny