routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new { controller = new ValueConstraint(x => x.ToUpper() != "WCF") }
);
public class ValueConstraint : IRouteConstraint
{
private Func<string, bool> _match;
public ValueConstraint(Func<string, bool> match)
{
_match = match;
}
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
return _match("" + values[parameterName]);
}
}
順便增加了 HttpContextBase 的處理
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new { authenticated = new HttpConstraint(x => x.Request.IsAuthenticated) }
);
public class HttpConstraint : IRouteConstraint
{
private Func<HttpContextBase, bool> _match;
public HttpConstraint(Func<HttpContextBase, bool> match)
{
_match = match;
}
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
return _match(httpContext);
}
}
沒有留言:
張貼留言
你好!歡迎你在我的 Blog 上留下你寶貴的意見。