Plug-in development of ASP.NET Core MVC(1) – Dynamically load controllers and views using ApplicationPart

Plug-in development of ASP.NET Core MVC(2) How to create a project template

Plug-in development of ASP.NET Core MVC(3) – How to enable components at runtime

Plug-in development of ASP.NET Core MVC(4) – Plugin installation

Plug-in development of ASP.NET Core MVC(5)- Plugin removal and upgrade

Plug-in development of ASP.NET Core MVC(6) – How to load plugin references

Source:
HTTPS ://github.com/lamondlu/Mystique

In the previous article, we introduced a simple example to show you how to develop a plugin in ASP.NET Core Mvc with ApplicationPart. At the end of the article, we also raised some issues to be resolved.

First of all, the first problem to solve is that every time we create a new plugin, we have to recreate the project each time, add a class library reference, and manually modify the csproj file, which is very cumbersome. So is there a way to reduce this part of the operation?

Recall that when we use Visual Studio to create a project, we can choose a project template. Visual Studio can not only create projects based on the template we choose, but also the assembly references we need are pre-configured. Need us to repeat the configuration. So can we create some templates ourselves? The answer is yes.

In this article, I will show you how to create a project template for a .NET Core project and simplify the process.

Write template.json

If we want to create a project template, we need to do 2 things.

  • Create a project code as a template
  • Write a template.json file

Here we have already created a DemoPlugin1 project, so this time we can convert it directly into a template project.

First, we add a project in the current .template.configdirectory, and add a template.json file. The content is as follows:

Copy{
    "author": "Lamond Lu", 
    "classifications": [ "Web/Plugins" ],
    "name": "DemoPlugin1", 
    "identity": "LamondDynamicPlugins", 
    "shortName": "ldp", 
    "tags": {
      "language": "C#" ,
      "type":"project"
    },
    "sourceName": "DemoPlugin1",  
    "preferNameDirectory": true
}

Configuration instructions:

  • AuthorThe attribute indicates the author of the template, required
  • classificationsThe attribute indicates the classification of the template, required
  • nameIndicates the name of the template. When you use the dotnet newcreated project, the specified template name is required.
  • identityIndicates the unique name of the template, optional
  • shortNameIndicates the short name of the template, required
  • tagsSpecified some labels for the current template
  • sourceNameA string is specified. When the dotnet new-n parameter is specified, the value of the -n parameter replaces the sourceNamespecified string. Take the current project as an example. Our project file name and all used namespaces are DemoPlugin1, but if we can’t use this name for new projects, we need to replace them with the project name specified by the -n parameter. DemoPlugin1″, if dotnet newthe -n parameter value specified by the current command is DemoPlugin2, when the project is created successfully, all the namespaces in the project and the project file name will use the new DemoPlugin2.
  • preferNameDirectory, whether to generate a directory with the same name, optional.

For template.jsondetailed configuration lists, interested students can view the following link
<https://github.com/dotnet/templating/wiki/Reference-for-template.json>

Create a template using dotnet-CLI

After writing the code, we can use the following dotnet-CLIcommands to install the project template.

Copydotnet new -i <PATH>

This <PATH>specifies the directory where the current template project is located.

After running the command, a list of the latest local templates is output, and we can see that it DemoPlugin1has appeared in the list as a template.


Plug-in_development_of_ASP.NET_Core_MVC(2)_How_to_create_a_project_template_1.png

 

Use to dotnet newcreate a project

Once the template is installed locally, we can use the dotnet newcommand to create the project. For example, if we want to create a new plugin now, we can use the following command.

Copydotnet new ldp -n DemoPlugin2

After running the command, the corresponding code is generated. Coming to the project directory, we found that the previous DemoPlugin1.csprojfile has been renamed DemoPlugin2.csproj, which means template.jsonthat the sourceNameproperties previously specified in the file helped me to correctly replace the project file name.


Plug-in_development_of_ASP.NET_Core_MVC(2)_How_to_create_a_project_template_2.png

 

Later, we can look at the only controller in a code and its namespace is replaced correctly.

how about it? Is this much simpler than the manual method?


Plug-in_development_of_ASP.NET_Core_MVC(2)_How_to_create_a_project_template_3.png

 

Note: If you want to uninstall the template you just installed, you need to use the dotnet new –debug:reinitcommand, the dotnet new -ucommand can not be uninstalled here, dotnet new -uonly for the template package downloaded from nuget.org

How to package a template into a Nuget package

In addition to the above way of creating project templates using project catalogs, we can also package templates into Nuget packages for others to use.

If you want to create a template, here we first need to create a Nuget package metadata file, here we namedLamondDynamicPlugin.nuspec

Copy<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
  <metadata>
    <id>LamondDynamicPlugin</id>
    <version>1.0.0</version>
    <description>
      Creates dynamic plugin in asp.net core
    </description>
    <authors>Lamond Lu</authors>
    <packageTypes>
      <packageType name="Template" />
    </packageTypes>
  </metadata>
</package>

The properties set here are very simple, the only thing you need to pay attention packageTypeto is set to Template.

After writing the metadata file, we can use the Nuget packcommand to package it. The first parameter of this command is to specify .nuspecthe path where a file is located.

Copynuget pack DemoPlugin1/LamondDynamicPlugin.nuspec


Plug-in_development_of_ASP.NET_Core_MVC(2)_How_to_create_a_project_template_4.png

 

After running, Nuget will generate a LamondDynamicPlugin.nupkgfile, this is what we call the Nuget package.

In order to install the template using the Nuget package, we can use the same commanddotnet new -i

Copydotnet new -i LamondDynamicPlugin.nupkg

Of course, we usually publish this Nuget package to nuget.org so that we can share this template with others.


Plug-in_development_of_ASP.NET_Core_MVC(2)_How_to_create_a_project_template_5.png

 

After publishing to Nuget, we can LamondDynamicPlugin.nuspecinstall the template using the unique id defined in it. The installed commands are the same as the previous installation template from the directory.

Copydotnet new -i LamondDynamicPlugin

If you want to uninstall a template, just use it dotnet new -u [Nuget包].

Copydotnet new -u LamondDynamicPlugin

Summary

In this article I demonstrated how to dotnet-CLIcreate a .NET Core project template and how to use Nuget to package project templates for others to use, which solves the cumbersome problem of manually creating plug-ins. In the next issue, I will try to abstract the business logic of the module and create a core class library. Please stay tuned.

Orignal link:https://www.cnblogs.com/lwqlun/p/11155666.html