Templates Available for dotnet new command

Early this week Microsoft announced the release of updates to .NET Core Tooling included in the Visual Studio 2017 RC which brings some major changes in the tool set. You can refer this link to read about the official release of the update. One of the major change is in creating new projects using the dotnet new command which is widely used by novice and experienced hands in their day to day life as a developer. According to the release note, the updated dotnet command will be having the following characteristics.

Platform — can be used by tools like Visual Studio and generator-aspnet (think yo aspnet).

The major change in dotnet new is that it is based on a templating engine which will create the artifacts automatically depending upon the projects you chose to create. To view the options available in the command, just execute the following command in the prompt.

x

1

dotnetnew--help

Creating Console Application.

Earlier we were using just dotnet new command to create a new console project in .NET Core, but with this iterating the syntax is little bit changed.

x

1

dotnetnewconsole

Executing the above command will create two files for you, one is the file with source code and the other is a csproj file. By default .NET Core runtime will use C# as the language for creating the project, if you want to specify a different one use the -l option along with the new command. You may already know that .NET Core is now reverted back to xml based to project file from the json based one since RC3 and that's why you are seeing a csproj file in the folder. Again by default, the runtime creates the csproj file in the name of the current folder.

Let's examine the contents of both the files

csproj file

The project file is much leaner now which basically contains the output type, target framework in it. You can read more about the changes here.

1

1

<ProjectSdk="Microsoft.NET.Sdk">

2

​

3

<PropertyGroup>

4

<OutputType>Exe</OutputType>

5

<TargetFramework>netcoreapp1.0</TargetFramework>

6

</PropertyGroup>

7

​

8

</Project>

Program.cs

The source file is still the same with Console.WriteLine() method in the main method.

1

10

1

namespaceAddPackagageSample

2

{

3

classProgram

4

{

5

staticvoidMain(string[] args)

6

{

7

Console.WriteLine("Hello World!");

8

}

9

}

10

}

Specifying Target Framework

To specify a different target framework while creating a new project, use --framework switch along with the dotnet new command

Syntax

1

1

dotnetnew<templatename>--framework<targetframework>

Usage

1

1

dotnetnewconsole--frameworknetcoreapp1.1

So if you inspect the contents of the csproj file, you will see that the TargetFramework tag will have the value which we passed in the command.

1

1

<ProjectSdk="Microsoft.NET.Sdk">

2

​

3

<PropertyGroup>

4

<OutputType>Exe</OutputType>

5

<TargetFramework>netcoreapp1.1</TargetFramework>

6

</PropertyGroup>

7

​

8

</Project>

Specifying Folder Location for Project

Let's see how we can create an ASP.NET Core MVC application in particular folder in the machine. To specify the location you need to use the -o switch followed by the name of the folder. If no path is specified then the folder will be created under the current directory.

1

1

dotnetnew--frameworknetcoreapp1.1-omvc

The contents of csproj file will be little more than the console project due to the references for the packages by default.