Window batch and macOS/Linux shell scripts to create new ASP.NET Core Project with class library and tests.

---
title: Create new ASP.NET Core Project with Class Library & Tests
author: Benjamin Day
date: February 10, 2017
source: https://www.benday.com/2017/02/10/script-create-new-asp-net-core-project-with-class-library-tests/
notoc: false
---

## Summary

Building on my [post about how to do some common Solution and Project tasks using the dotnet command](https://www.benday.com/2017/02/10/cheat-sheet-use-dotnet-exe-to-create-new-solutions-projects-references/),
here's a sample script to create a complete solution that can be used from
Visual Studio 2017 or Visual Studio Code.

I wanted to be able to create a complete solution and project structure all from
the command line.  Yes yes yes, I know that I could do something like "yo
aspnet" and that that would pretty much do the same thing.  Well, it only
*pretty much* does the same thing and I wanted to be able to do everything start
to finish and only using the dotnet command.  I wanted to create the solution
file (`*.sln`), create the folder structure, create a project for the ASP.NET
Core code, create a class library project that would hold the back-end code that
would be used by the ASP.NET Core project, create an MSTest-based unit test
project, and then create all the references between projects so that this would
compile right away.  I wanted to be able to call my script, pass in an argument
for the Solution Name and then have it create everything.

## Directory structure

Here's the **directory structure** that I wanted:

    /MySolution
        MySolution.sln
      /src
        /MySolution.Api
          MySolution.Api.csproj
        /MySolution.WebUi
          MySolution.WebUi.csproj
      /test
        /MySolution.Tests
          MySolution.Tests.csproj

## Example usage

Assuming a script file called `Create.bat` and a solution name of `MySolution`,
here's the example call:

    Create.bat MySolution

## Windows batch script

Here's the **batch script** for doing this on **Windows**:

    set "base=%cd%"
    set "workspaceroot=%base%\%1"

    echo %base%
    echo %workspaceroot%

    mkdir %workspaceroot%
    mkdir %workspaceroot%\src
    mkdir %workspaceroot%\src\%1.Api
    mkdir %workspaceroot%\src\%1.WebUi

    mkdir %workspaceroot%\test
    mkdir %workspaceroot%\test\%1.Tests

    cd %workspaceroot%\src\%1.Api
    dotnet new classlib -f netcoreapp1.1

    cd %workspaceroot%\src\%1.WebUi
    dotnet new mvc -f netcoreapp1.1

    cd %workspaceroot%\test\%1.Tests
    dotnet new mstest -f netcoreapp1.1

    cd %workspaceroot%
    dotnet new sln
    dotnet sln .\%1.sln add .\src\%1.Api\%1.Api.csproj
    dotnet sln .\%1.sln add .\src\%1.WebUi\%1.WebUi.csproj
    dotnet sln .\%1.sln add .\test\%1.Tests\%1.Tests.csproj

    cd %workspaceroot%\src\%1.WebUi
    dotnet add reference ..\%1.Api\%1.Api.csproj

    cd %workspaceroot%\test\%1.Tests
    dotnet add reference ..\..\src\%1.Api\%1.Api.csproj
    dotnet add reference ..\..\src\%1.WebUi\%1.WebUi.csproj

    cd %workspaceroot%
    dotnet restore
    dotnet build

## macOS and Linux shell script

Here's the **shell script** for doing this on **macOS and Linux**:

    #!/bin/bash

    base="$PWD"
    workspaceroot="$base/$1"

    echo  $workspaceroot

    mkdir $workspaceroot
    mkdir $workspaceroot/src
    mkdir $workspaceroot/src/$1.Api
    mkdir $workspaceroot/src/$1.WebUi

    mkdir $workspaceroot/test
    mkdir $workspaceroot/test/$1.Tests

    cd $workspaceroot/src/$1.Api
    dotnet new classlib -f netcoreapp1.1

    cd $workspaceroot/src/$1.WebUi
    dotnet new mvc -f netcoreapp1.1

    cd $workspaceroot/test/$1.Tests
    dotnet new mstest -f netcoreapp1.1

    cd $workspaceroot
    dotnet new sln
    dotnet sln ./$1.sln add ./src/$1.Api/$1.Api.csproj
    dotnet sln ./$1.sln add ./src/$1.WebUi/$1.WebUi.csproj
    dotnet sln ./$1.sln add ./test/$1.Tests/$1.Tests.csproj

    cd $workspaceroot/src/$1.WebUi
    dotnet add reference ../$1.Api/$1.Api.csproj

    cd $workspaceroot/test/$1.Tests
    dotnet add reference ../../src/$1.Api/$1.Api.csproj
    dotnet add reference ../../src/$1.WebUi/$1.WebUi.csproj

    cd $workspaceroot
    dotnet restore
    dotnet build

## Download the scripts

- Windows batch script ([Download](https://www.benday.com/blogfiles/createaspnetcoresolution/CreateWebProject-core.bat.txt))
- macOS and Linux shell script ([Download](https://www.benday.com/blogfiles/createaspnetcoresolution/CreateWebProject-core.sh.txt))