我认为路由约束实现在“entry/”后面传递所有字符串,但除了 view、edit 和 new 之类的词,因此“默认”路由可以处理。就像是:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"EntryView",
"Entry/{identifier}",
new { controller = "Entry", action = "View" },
new { identifier = new NotEqual(new string[]{"View", "Edit" , "New"}) }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
这是 NotEqual 类:
public class NotEqual : IRouteConstraint
{
private string[] _match;
public NotEqual(string[] match)
{
_match = match;
}
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
for (int i = 0; i < _match.Length; i++)
{
if (String.Compare(values[parameterName].ToString(), _match[i], true) == 0)
{
return false;
}
}
return true;
}
}
我对其进行了测试并且它有效,我在http://stephenwalther.com/archive/2008/08/07/asp-net-mvc-tip-30-create-custom-route-constraints.aspx上找到了它,因为我需要它