受欢迎的博客标签

View components in ASP.NET Core

Published

https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-6.0

https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-6.0

 

 

 

View components search path

The runtime searches for the view in the following paths:

视图搜索路径,运行时在以下路径中搜索视图:

/Views/{Controller Name}/Components/{View Component Name}/{View Name}
/Views/Shared/Components/{View Component Name}/{View Name}
/Pages/Shared/Components/{View Component Name}/{View Name}

 

Step 1:Create two folders  for  view component

Create the /Components folder

Create the Views/Shared/Components folder.

(This folder must be named Components.)

 

Step 2:Creating a view component

Add a ViewComponent class

/Components folder

using Microsoft.AspNetCore.Mvc;


namespace Miniblog.Core.Components
{
    public class BlogMonthsViewComponent : ViewComponent
    {
       

        public BlogMonthsViewComponent()
        {
         
        }

        public async Task<IViewComponentResult> InvokeAsync(
        int maxPriority, bool isDone)
        {
            
            return View();
        }
        
    }
}

 

Step 3:Invoking a view component

0

@await Component.InvokeAsync("PriorityList", new { maxPriority = 4, isDone = true })