public static void Configure(IRouteSource routeSource, IRouteMonitor routeMonitor = null) { if (routeSource == null) throw new ArgumentNullException("routeSource"); Register(RouteTable.Routes, routeSource.GetRouteItems()); if (routeMonitor != null) routeMonitor.Monitor(routeSource, Register); }
public void Monitor(IRouteSource routeSource, Action<RouteCollection, IEnumerable<RouteItem>> action) { _watcher = new FileSystemWatcher(HostingEnvironment.MapPath("~/"), _routeConfigFileName); _watcher.IncludeSubdirectories = true; _watcher.EnableRaisingEvents = true; _watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; Action a = () => { try { _watcher.EnableRaisingEvents = false; action(RouteTable.Routes, routeSource.GetRouteItems()); _watcher.EnableRaisingEvents = true; } catch { } // we don't want to put the server down if some exception happens. we have to do some logging here. }; _watcher.Created += delegate(object sender, FileSystemEventArgs e) { a(); }; _watcher.Deleted += delegate(object sender, FileSystemEventArgs e) { a(); }; _watcher.Changed += delegate(object sender, FileSystemEventArgs e) { a(); }; _watcher.Renamed += delegate(object sender, RenamedEventArgs e) { a(); }; }