public static void RegisterMvcServices(this IWebBuilder webBuilder) { webBuilder.RegisterServices(services => { services.AddMvc(); }); }
public ExporterTests() { _webBuilder = new Web.Impl.WebBuilder(new HostBuilder()); _hostExporter = new HostExporter(); _directory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Guid.NewGuid().ToString()); Directory.CreateDirectory(_directory); }
private static void RegisterFileInfo(this IWebBuilder webBuilder, IFileProvider fileProvider, PathString prefix, string basePath, IFileInfo fileInfo, RegisterOptions options) { if (fileInfo.IsDirectory) { var content = fileProvider.GetDirectoryContents(Path.Combine(basePath, fileInfo.Name)); if (content == null || !content.Exists) { return; } foreach (var child in content) { webBuilder.RegisterFileInfo(fileProvider, prefix, Path.Combine(basePath, fileInfo.Name), child, options); } } else { var filePath = Path.Combine(basePath, fileInfo.Name); var requestPath = new PathString().Add(prefix) .Add(filePath); if (options != null && options.Matcher != null) { if (!options.Matcher.Match(filePath.Substring(1)).HasMatches) { // We are ignoring this file return; } } var builtState = options?.State?.Invoke(prefix, requestPath, filePath, fileInfo, fileProvider); webBuilder.Register(requestPath, async context => { var env = context.RequestServices.GetRequiredService <IHostingEnvironment>(); var statileFileOptions = Options.Create(new StaticFileOptions()); statileFileOptions.Value.FileProvider = fileProvider; statileFileOptions.Value.ServeUnknownFileTypes = true; var loggerFactory = context.RequestServices.GetRequiredService <ILoggerFactory>(); var middleware = new StaticFileMiddleware(_ => Task.CompletedTask, env, statileFileOptions, loggerFactory); var oldPath = context.Request.Path; try { context.Request.Path = filePath; await middleware.Invoke(context); } finally { context.Request.Path = oldPath; } }, builtState, /*don't convert "/file" to "/file/index.html"*/ true); } }
private static async Task RegisterPages(IWebBuilder webBuilder, string rootDirectory) { var pages = await LoadPages(rootDirectory); var root = new MenuItem { Path = "/", Title = "Home" }; // Register the root node so we can get it later. webBuilder.RegisterServices(services => services.AddSingleton(root)); void Walk(PageTreeItem <PageNode> pageItem, MenuItem menuItem) { if (pageItem.Data == null) { // There isn't a markdown file associated with this level. // Maybe missing index.md? webBuilder.RegisterMvc(pageItem.Path, new { controller = "Page", action = "EmptyParent", menu = menuItem }, new PageOptions(null, menuItem.Title)); } else { webBuilder.RegisterMvc(pageItem.Path, new { controller = "Page", action = "Page", page = pageItem.Data, menu = menuItem }, new PageOptions(pageItem.FilePath, menuItem.Title)); } foreach (var child in pageItem.Children) { var childMenuItem = new MenuItem(); childMenuItem.Title = child.Data.Title; childMenuItem.Path = child.Path; childMenuItem.Parent = menuItem; childMenuItem.Order = child.Data.Order; childMenuItem.Level = menuItem.Level + 1; menuItem.Children.Add(childMenuItem); Walk(child, childMenuItem); } } Walk(pages, root); }
public static void RegisterFileProvider(this IWebBuilder webBuilder, PathString prefix, IFileProvider fileProvider, RegisterOptions options = null) { var contents = fileProvider.GetDirectoryContents("/"); if (contents == null || !contents.Exists) { return; } foreach (var file in contents) { webBuilder.RegisterFileInfo(fileProvider, prefix, "/", file, options); } }
private static async Task RegisterConfig(IWebBuilder webBuilder, string rootDirectory) { var configFile = Path.Combine(rootDirectory, "config.yml"); string configContents; try { configContents = await File.ReadAllTextAsync(configFile); } catch (Exception ex) { throw new Exception($"Problem loading file {configFile}", ex); } var parsed = Parser.Parse($"---\n{configContents}\n---"); var config = new ProjectConfig { Name = parsed.Yaml.name, Logo = parsed.Yaml.logo, EditUrl = parsed.Yaml.edit_url, Footer = parsed.Yaml.footer, GoogleTrackingId = parsed.Yaml.google_tracking_id }; var moreLinks = new List <MoreLink>(); JArray links = parsed.Yaml.more_links; if (links != null) { foreach (dynamic link in links) { moreLinks.Add(new MoreLink { Text = link.text, Url = link.url, Icon = link.icon }); } } webBuilder.RegisterServices(services => { services.AddSingleton(moreLinks); services.AddSingleton(config); }); }
private static void RegisterTopics(IWebBuilder builder, List <ChannelModel> channels) { builder.RegisterMvc("/topics", new { controller = "Topic", action = "List" }); foreach (var channel in channels) { builder.RegisterMvc($"/channel/{channel.Channel.Id}/topics", new { controller = "Topic", action = "ListByChannel", channelId = channel.Channel.Id }); } foreach (var topic in channels.SelectMany(x => x.Topics) .GroupBy(x => x.Id) .Select(x => x.First())) { builder.RegisterMvc($"/topic/{topic.Id}", new { controller = "Topic", action = "TopicVideos", topicId = topic.Id }, new PageState(topic.Topic)); } foreach (var channel in channels) { foreach (var topic in channel.Topics) { if (topic.Videos.Count > 0) { builder.RegisterMvc($"/channel/{channel.Channel.Id}/topic/{topic.Id}", new { controller = "Topic", action = "TopicVideosForChannel", topicId = topic.Id, channelId = channel.Channel.Id }, new PageState($"{topic.Topic} - {channel.Channel.Title}")); } } } }
private static void RegisterVideos(IWebBuilder builder, List <ChannelModel> channels) { foreach (var channel in channels) { foreach (var video in channel.Videos) { builder.RegisterMvc($"/video/{video.Video.Id}", new { controller = "Video", action = "Index", videoId = video.Video.Id, channelId = channel.Channel.Id }); } } }
public static void Redirect(this IWebBuilder webBuilder, string from, string to, object state = null, bool extractExactPath = false) { webBuilder.Register(from, async(context) => { context.Response.ContentType = "text/html"; await context.Response.WriteAsync($@"<!DOCTYPE html> <html lang=""en-US""> <meta charset=""utf-8""> <title>Redirecting…</title> <link rel=""canonical"" href=""{context.Request.PathBase}{to}""> <script>location=""{context.Request.PathBase}{to}""</script> <meta http-equiv=""refresh"" content=""0; url={context.Request.PathBase}{to}""> <meta name=""robots"" content=""noindex""> <h1>Redirecting…</h1> <a href=""{context.Request.PathBase}{to}"">Click here if you are not redirected.</a> </html>"); }, state, extractExactPath); }
public static void RegisterMvcServices(this IWebBuilder webBuilder, params Assembly[] additionalParts) { var callingAssembly = Assembly.GetCallingAssembly(); webBuilder.RegisterServices(services => { var parts = additionalParts.ToList(); if (!parts.Contains(callingAssembly)) { parts.Add(callingAssembly); } var b = services.AddMvc(); foreach (var part in parts) { b.AddApplicationPart(part); } b.AddRazorRuntimeCompilation(); }); }
public static void RegisterMvc(this IWebBuilder webBuilder, string path, object routeData, object state = null) { webBuilder.Register(path, async context => { var actionSelector = context.RequestServices.GetRequiredService <IActionSelector>(); var actionInvokerFactory = context.RequestServices.GetRequiredService <IActionInvokerFactory>(); var routeContext = new RouteContext(context); if (routeData != null) { foreach (var value in new RouteValueDictionary(routeData)) { routeContext.RouteData.Values[value.Key] = value.Value; } } var candidates = actionSelector.SelectCandidates(routeContext); if (candidates == null || candidates.Count == 0) { throw new Exception("No actions matched"); } var actionDescriptor = actionSelector.SelectBestCandidate(routeContext, candidates); if (actionDescriptor == null) { throw new Exception("No actions matched"); } var actionContext = new ActionContext(context, routeContext.RouteData, actionDescriptor); var invoker = actionInvokerFactory.CreateInvoker(actionContext); if (invoker == null) { throw new InvalidOperationException("Couldn't create invoker"); } await invoker.InvokeAsync(); }, state); }
public ManualWeb(IWebBuilder webBuilder) { _webBuilder = webBuilder; }
public MvcTests() { _webBuilder = new WebBuilder(new HostBuilder()); _webBuilder.RegisterMvcServices(); }
public static void RegisterDirectory(this IWebBuilder webBuilder, PathString prefix, string directory, RegisterOptions options = null) { RegisterFileProvider(webBuilder, prefix, new PhysicalFileProvider(directory), options); }
public WebBuilderTests() { _webBuilder = new Web.Impl.WebBuilder(new Hosting.Impl.HostBuilder()); }
public static void RegisterFileProvider(this IWebBuilder webBuilder, IFileProvider fileProvider, RegisterOptions options = null) { RegisterFileProvider(webBuilder, "/", fileProvider, options); }
public FileProviderTests() { _webBuilder = new WebBuilder(new HostBuilder()); }