View components let developers create a component with both server-side logic and client-side code to render layout.
- First, add a service and a TagCloud class in order to display a list of tag clouds in a reusable component:
public interface ITagCloudService { Task<List<Tag>> GetTagsAsync(string userBlog); } public class TagCloudService : ITagCloudService { public async Task<List<Tag>> GetTagsAsync(string userBlog) { return await Task.Run(() => GetTags(userBlog)); } private List<Tag> GetTags(string userBlog) { return new List<Tag> { new Tag { Id = 1, Name = "Asp.Net Core" }, new Tag { Id = 2, Name = "EF Core" } }; } } public class Tag { public int Id { get; set; } public string Name { get; set; } }
- In Sartup.cs, we configure ...