ایجاد فایل viewComponent به صورت اتوماتیک در پروژه های dotnet core

10/2/2025 MVC Core
35

مشکل من این بود که هر باز برای استفاده از ViewComponent ها باید یک پوشه در Pages/Shared/CiewcomponentName/Default.cshtml
درست کنیم از طرفی در پروژه یک پوشه به نام viewcomponents/ViewcomponentName.cs درست کنیم که یک کد اولیه در ان وجود دارد 
من معمولا در وب سایت های تک صفحه ای هر قسمت را به یک viewComponent استفاده میکنم

خوب برای اینکه ابتدا فایل های html با هم متفاوت هستند و فایل های viewComponent.name یکسان (البته بعد تغییر خواهند کرد و احتمالا اطلاعات را از بانک اطلاعاتی خواهند خواند و یا به بانک اطلاعتی ارسال خواهند کرد)
برای این کار این شل را ایجاد کردم که هم در ویندوز با استفاده از powershell هست و برای لینوکس هم کد bash


در ویندوز

# Ask for the project name
$projectname = Read-Host "Please enter your project name"

# Define the base directory
$base_dir = "${projectname}.Raz\Pages\Shared\Components"

# Create ViewComponents directory
$view_components_dir = "${projectname}.Raz\ViewComponents"
if (-Not (Test-Path $view_components_dir)) {
    New-Item -ItemType Directory -Path $view_components_dir
}

# Search for folders containing 'Default.cshtml' and process each
Get-ChildItem -Path $base_dir -Recurse -File -Filter "Default.cshtml" | ForEach-Object {
    # Extract the folder name (parent directory of Default.cshtml)
    $folder_name = Split-Path $_.DirectoryName -Leaf

    # Create a new ViewComponent .cs file
    $component_file = "${view_components_dir}\${folder_name}ViewComponent.cs"

    # Write the template code to the new .cs file
    $code = @"
using ${projectname}.Repo.Contracts;
using Microsoft.AspNetCore.Mvc;

namespace ${projectname}.Raz.ViewComponents;

public class ${folder_name}ViewComponent: ViewComponent
{
    //private readonly ISiteContentRepository _siteContentRepository1;
    //public ${folder_name}ViewComponent(ISiteContentRepository siteContentRepository) 
    //{
    //    _siteContentRepository1 = siteContentRepository;
    //}

    public async Task<IViewComponentResult> InvokeAsync()
    {
        //var item = await _siteContentRepository1.GetByIdAsync(1);
        return View();
    }
}
"@

    # Save the code to the component file
    Set-Content -Path $component_file -Value $code

    Write-Host "Created ViewComponent: ${folder_name}ViewComponent.cs"
}

Write-Host "All ViewComponent files have been created."

 

در لینوکس

#!/bin/bash

# Ask for the project name
echo "Please enter your project name:"
read projectname

# Define the base directory
base_dir="${projectname}.Raz/Pages/Shared/Components"

# Create ViewComponents directory
mkdir -p "${projectname}.Raz/ViewComponents"

# Search for directories containing 'Default.cshtml' and process each
find "$base_dir" -type f -name "Default.cshtml" | while read -r file; do
    # Extract the folder name (parent directory of Default.cshtml)
    folder_name=$(dirname "$file" | xargs basename)

    # Create a new ViewComponent .cs file
    component_file="${projectname}.Raz/ViewComponents/${folder_name}ViewComponent.cs"

    # Write the template code to the new .cs file
    cat <<EOF > "$component_file"
using ${projectname}.Repo.Contracts;
using Microsoft.AspNetCore.Mvc;

namespace ${projectname}.Raz.ViewComponents;

public class ${folder_name}ViewComponent: ViewComponent
{
    //private readonly ISiteContentRepository _siteContentRepository1;
    //public ${folder_name}ViewComponent(ISiteContentRepository siteContentRepository) 
    //{
    //    _siteContentRepository1 = siteContentRepository;
    //}

    public async Task<IViewComponentResult> InvokeAsync()
    {
        //var item = await _siteContentRepository1.GetByIdAsync(1);
        return View();
    }
}
EOF

    echo "Created ViewComponent: ${folder_name}ViewComponent.cs"
done

echo "All ViewComponent files have been created."

 

 

فقط در لینوکس به یاد داشته باشید که دسترسی به فایل بدهید بعد ان را اجرا کنید 

chmod +x vc.sh

و اجرا 

./vc.sh