/// <summary> /// Finds the match. /// </summary> /// <param name="url">The URL.</param> /// <param name="context">The context.</param> /// <returns></returns> public RouteMatch FindMatch(string url, IRouteContext context) { // TODO: lock for reading var winnerPoints = 0; RouteMatch winner = null; DecoratedRule winnerule = null; foreach (var rule in rules) { var match = new RouteMatch { Name = rule.RouteName }; var points = rule.Matches(url, context, match); if (points != 0 && points > winnerPoints) { winnerPoints = points; winner = match; winnerule = rule; } } if (winner != null && winnerule.SelectionAction != null) { winnerule.SelectionAction(context, winner); } return(winner); }
public void Request_CreatesSessionfulHandler() { StringWriter writer = new StringWriter(); HttpResponse res = new HttpResponse(writer); HttpRequest req = new HttpRequest(Path.Combine( AppDomain.CurrentDomain.BaseDirectory, "Handlers/Files/simplerequest.txt"), "http://localhost:1333/home/something", ""); RouteMatch routeMatch = new RouteMatch(); HttpContext httpCtx = new HttpContext(req, res); httpCtx.Items[RouteMatch.RouteMatchKey] = routeMatch; using(mockRepository.Record()) { ControllerMetaDescriptor controllerDesc = new ControllerMetaDescriptor(); controllerDesc.ControllerDescriptor = new ControllerDescriptor(typeof(Controller), "home", "", false); Expect.Call(controllerFactoryMock.CreateController("", "home")).IgnoreArguments().Return(controllerMock); Expect.Call(controllerDescriptorProviderMock.BuildDescriptor(controllerMock)).Return(controllerDesc); ControllerContext controllerContext = new ControllerContext(); controllerContext.ControllerDescriptor = new ControllerMetaDescriptor(); Expect.Call(controllerContextFactoryMock.Create("", "home", "something", controllerDesc, routeMatch)). Return(controllerContext); } using(mockRepository.Playback()) { IHttpHandler handler = handlerFactory.GetHandler(httpCtx, "GET", "", ""); Assert.IsNotNull(handler); Assert.IsInstanceOf(typeof(MonoRailHttpHandler), handler); } }
private string CreateMonoRailPath(RouteMatch match) { if (!match.Parameters.ContainsKey("controller")) { throw new MonoRailException( "Parameter 'controller' is not optional. Check your routing rules to make " + "sure this parameter is being added to the RouteMatch"); } if (!match.Parameters.ContainsKey("action")) { throw new MonoRailException( "Parameter 'action' is not optional. Check your routing rules to make " + "sure this parameter is being added to the RouteMatch"); } var controller = match.Parameters["controller"]; var area = match.Parameters.ContainsKey("area") ? match.Parameters["area"] : null; var action = match.Parameters["action"]; if (area != null) { return("~/" + area + "/" + controller + "/" + action + defaultUrlExtension); } else { return("~/" + controller + "/" + action + defaultUrlExtension); } }
public int Matches(string url, Castle.MonoRail.Framework.IRouteContext context, RouteMatch match) { if (context.Request.HttpMethod == _httpMethod) { return _innerRoute.Matches(url, context, match); } return 0; }
public override bool Matches(string part, RouteMatch match, ref int points) { if (part == null) { points = 1; return(true); } return(false); }
/// <summary> /// Determines if the specified URL matches the /// routing rule. /// </summary> /// <param name="url">The URL.</param> /// <param name="context">The context</param> /// <param name="match">The match.</param> /// <returns></returns> public int Matches(string url, IRouteContext context, RouteMatch match) { if (url.ToLowerInvariant() == urlToMatch.ToLowerInvariant()) { context.Response.Redirect(redirectUrl); return(100); } return(0); }
/// <summary> /// Determines if the specified URL matches the /// routing rule. /// </summary> /// <param name="url">The URL.</param> /// <param name="context">The context</param> /// <param name="match">The match.</param> /// <returns></returns> public int Matches(string url, IRouteContext context, RouteMatch match) { if (url.ToLowerInvariant() == urlToMatch.ToLowerInvariant()) { context.Response.Redirect(redirectUrl); return 100; } return 0; }
/// <summary> /// Pendent. /// </summary> /// <param name="container"></param> /// <param name="urlInfo"></param> /// <param name="context"></param> /// <param name="routeMatch"></param> /// <returns></returns> public IEngineContext Create(IMonoRailContainer container, UrlInfo urlInfo, HttpContext context, RouteMatch routeMatch) { IDictionary session = ResolveRequestSession(container, urlInfo, context); IUrlBuilder urlBuilder = container.UrlBuilder; ServerUtilityAdapter serverUtility = new ServerUtilityAdapter(context.Server); string referrer = context.Request.Headers["Referer"]; return new DefaultEngineContext(container, urlInfo, context, serverUtility, new RestfulRequestAdapter(context.Request), new ResponseAdapter(context.Response, urlInfo, urlBuilder, serverUtility, routeMatch, referrer), new TraceAdapter(context.Trace), session); }
/// <summary> /// Determines if the specified URL matches the /// routing rule. /// </summary> /// <param name="url">The URL.</param> /// <param name="context">The context</param> /// <param name="match">The match.</param> /// <returns></returns> public virtual int Matches(string url, IRouteContext context, RouteMatch match) { if (verbs.HasValue) { var requestVerb = (Verb)Enum.Parse(typeof(Verb), context.Request.HttpMethod, true); if ((verbs.Value & requestVerb) == 0) { return(0); } } var parts = GetUrlParts(url); var points = 0; var index = 0; foreach (var node in nodes) { var part = index < parts.Length ? parts[index] : null; if (!node.Matches(part, match, ref points)) { points = 0; break; } index++; } if (points != 0) { // Fills parameters set on the route that cannot be fulfilled by the url foreach (var pair in defaults) { if (!match.Parameters.ContainsKey(pair.Key)) { match.Parameters.Add(pair.Key, pair.Value); } } } return(points); }
public override int Matches(string url, IRouteContext context, RouteMatch match) { var path = context.Request.Uri .GetComponents(UriComponents.Path, UriFormat.Unescaped); if (Normalise(path).Equals(Normalise(context.ApplicationPath)) == false) { return 0; } foreach (KeyValuePair<string, string> pair in getDefaults(this)) { if (!match.Parameters.ContainsKey(pair.Key)) { match.Parameters.Add(pair.Key, pair.Value); } } return 100; }
public virtual bool Matches(string part, RouteMatch match, ref int points) { if (part == null) { if (optional) { if (name != null) { // matching defaults is better than nothing, so // assign at least a very small number of points points += 1; match.AddNamed(name, defaultVal); } return(true); } else { return(false); } } var regExpMatch = exp.Match(part); if (regExpMatch.Success) { if (name != null) { match.AddNamed(name, part); } // matching non-default nodes is preferred, static nodes even more, // so assign a high number of points. // By using very high values it is ensured that defaults are never // preferred over non-default matches points += isStaticNode ? 4000 : 2000; return(true); } return(false); }
public int Matches(string url, IRouteContext context, RouteMatch match) { string[] parts = GetParts(url); if (parts.Length != routeParts.Length) { return 0; } for (int i = 0; i < parts.Length; i++) { if (string.Compare(parts[i], routeParts[i], true) != 0) { return 0; } } match.Parameters.Add("area", area); match.Parameters.Add("controller", controller); match.Parameters.Add("action", action); return 100; }
private string CreateMonoRailPath(RouteMatch match) { if (!match.Parameters.ContainsKey("controller")) { throw new MonoRailException( "Parameter 'controller' is not optional. Check your routing rules to make " + "sure this parameter is being added to the RouteMatch"); } if (!match.Parameters.ContainsKey("action")) { throw new MonoRailException( "Parameter 'action' is not optional. Check your routing rules to make " + "sure this parameter is being added to the RouteMatch"); } var controller = match.Parameters["controller"]; var area = match.Parameters.ContainsKey("area") ? match.Parameters["area"] : null; var action = match.Parameters["action"]; if (area != null) { return "~/" + area + "/" + controller + "/" + action + defaultUrlExtension; } else { return "~/" + controller + "/" + action + defaultUrlExtension; } }
public void RedirectUsingRoute_SpecifyingParameters() { engine.Add(new PatternRoute("/something/<param1>/admin/[controller]/[action]/[id]")); RouteMatch match = new RouteMatch(); UrlInfo url = new UrlInfo("area", "home", "index", "", ".castle"); StubResponse response = new StubResponse(url, urlBuilder, urlBuilder.ServerUtil, match); response.RedirectUsingRoute("cart", "checkout", DictHelper.Create("param1=Marge")); Assert.AreEqual("/something/Marge/admin/cart/checkout", response.RedirectedTo); }
public void RedirectUsingRoute_InheritingParameters() { engine.Add(new PatternRoute("/something/<param1>/admin/[controller]/[action]/[id]")); RouteMatch match = new RouteMatch(); match.AddNamed("param1", "Homer"); UrlInfo url = new UrlInfo("area", "home", "index", "", ".castle"); StubResponse response = new StubResponse(url, urlBuilder, urlBuilder.ServerUtil, match); response.RedirectUsingRoute("cart", "checkout", true); Assert.AreEqual("/something/Homer/admin/cart/checkout", response.RedirectedTo); }
public int Matches(string url, IRouteContext context, RouteMatch match) { return inner.Matches(url, context, match); }
/// <summary> /// Finds the match. /// </summary> /// <param name="url">The URL.</param> /// <param name="context">The context.</param> /// <returns></returns> public RouteMatch FindMatch(string url, IRouteContext context) { // TODO: lock for reading int winnerPoints = 0; RouteMatch winner = null; DecoratedRule winnerule = null; foreach(DecoratedRule rule in rules) { RouteMatch match = new RouteMatch(); int points = rule.Matches(url, context, match); if (points != 0 && points > winnerPoints) { winnerPoints = points; winner = match; winnerule = rule; } } if (winner != null && winnerule.SelectionAction != null) { winnerule.SelectionAction(context, winner); } return winner; }
public int Matches(string url, IRouteContext context, RouteMatch match) { return(inner.Matches(url, context, match)); }
void InitUrlInfo(string areaName, string controllerName, string actionName) { var urlInfo = new UrlInfo(areaName, controllerName, actionName, "/", "castle"); SetupResult.For(engineContext.UrlInfo).Return(urlInfo); var routeMatch = new RouteMatch(); SetupResult.For(controllerContext.RouteMatch).Return(routeMatch); }
protected void InitUrlInfo(string areaName, string controllerName, string actionName) { var urlInfo = new UrlInfo(areaName, controllerName, actionName, "/", "castle"); engineContext = new StubEngineContext(); engineContext.AddService(typeof(IUrlBuilder), serviceProvider.UrlBuilder); engineContext.CurrentController = controller; engineContext.CurrentControllerContext = controllerContext; engineContext.Services.ViewEngineManager = serviceProvider.ViewEngineManager; output = (StringWriter) engineContext.Response.Output; var routeMatch = new RouteMatch(); controllerContext.RouteMatch = routeMatch; }