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); }
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 async Task Can_call_mvc_action() { _webBuilder.RegisterMvc("/somewhere", new { controller = "Test", action = "Index" }); using (var host = _webBuilder.BuildVirtualHost()) { using (var client = host.CreateClient()) { var responseMessage = await client.GetAsync("/somewhere"); responseMessage.EnsureSuccessStatusCode(); var response = await responseMessage.Content.ReadAsStringAsync(); Assert.Equal("test", response); } } }