一个疯狂的猜测:
可能您的路线是在默认路线之后注册的。将它作为第一条路线放在你的 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
);
}
更新
@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")
}
);