In this recipe, we will learn how to create a view component.
We create an empty web application, adding Microsoft.AspNetCore.Mvc
in project.json
. We also create a controller class with an Index View.
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...