how to write custom template in aspnet code generator?

mc 5,426 Reputation points
2023-06-29T06:37:24.64+00:00

aspnet-codegenerator tool to generate a razor pages how to write my custom code template?

Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-06-30T06:29:05.2833333+00:00

    Hi @mc

    From your description, you want to custom scaffold templates in ASP.NET Core, right? If that is the case, you can refer to this tutorial: Custom scaffold templates in ASP.NET Core, or refer to the following steps:

    By default, the code generators template is in this folder - %USERPROFILE%\.nuget\packages\microsoft.visualstudio.web.codegenerators.mvc\6.0.6\Templates (the number 6.0.6 will change based on the SDK version).

    1. Create a Asp.net core Razor or MVC project.
    2. In your project, you can create a folder with name Templates, then copy the templates from the default folder to your project folder. Then, modify the templates in your project and modify the project file like this.
          <ItemGroup>
            <Compile Remove="Templates\**" />
            <Content Remove="Templates\**" />
            <EmbeddedResource Remove="Templates\**" />
            <None Remove="Templates\**" />
          </ItemGroup>
    
    1. Use this command to install the dotnet-aspnet-codegenerator:
    dotnet tool install -g dotnet-aspnet-codegenerator
    
    1. using the command to install the Microsoft.VisualStudio.Web.CodeGeneration.Design nuget package to the project:
    dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
    
    1. Open the project folder in File Explorer. Right click the project and select the "Open Folder in File Explorer" option. Then, delete the bin and obj folder, after that rebuild the project.
    2. Now we can scaffold the controller using the following command, it will generate pages based on the custom templates:
    dotnet-aspnet-codegenerator controller -name ExampleController -outDir Controllers --no-build
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.