# Solution creation

## Example

I want to create a solution called `KnowYourAge` and then create 2 projects inside it, first one is a a razor pages app called WebApp and another project called CoreLib which is a class library, using **command line interface and VS code**, how do I do it?

<div align="left"><figure><img src="https://2166276527-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsIIbWt9PgxsTyKOjFPJr%2Fuploads%2FgPXc1JHJMN1i71dDLCDE%2Fimage.png?alt=media&#x26;token=7111b32a-37b0-4346-917d-7fc17043a55c" alt=""><figcaption><p>Solution structure in VSCode</p></figcaption></figure></div>

### Create solution folder

1. Open a terminal or command prompt window.
2. Create a new directory for your solution, and navigate into it:

```bash
mkdir KnowYourAge
cd KnowYourAge
```

3. Create a new solution file called `KnowYourAge.sln` using the .NET Core CLI:

```arduino
dotnet new sln -n KnowYourAge
```

4. Create a new Razor Pages app called `WebApp` using the .NET Core CLI:

```arduino
dotnet new webapp -n WebApp
```

This will create a new folder called `WebApp` with a new Razor Pages app inside it.

### Add project to solution

5. Add the `WebApp` project to the `KnowYourAge` solution:

```csharp
dotnet sln add WebApp/WebApp.csproj
```

6. Create a new class library project called `CoreLib` using the .NET Core CLI:

```arduino
dotnet new classlib -n CoreLib
```

This will create a new folder called `CoreLib` with a new class library project inside it.

7. Add the `CoreLib` project to the `KnowYourAge` solution:

```csharp
dotnet sln add CoreLib/CoreLib.csproj
```

8. Build the solution to make sure everything is working:

```
dotnet build KnowYourAge.sln
```

### Add reference of another project

I also want to add reference of the class library on to my web application project using CLI, how do i do it?

1. Open a terminal or command prompt window.
2. Navigate to the root directory of your `WebApp` project:

```bash
cd /path/to/KnowYourAge/WebApp
```

3. Add a reference to the `CoreLib` project using the .NET Core CLI:

```csharp
dotnet add reference ../CoreLib/CoreLib.csproj
```

This will add a project reference to the `CoreLib` project from the `WebApp` project.

4. Save your changes and rebuild the `WebApp` project:

```
dotnet build
```

This will rebuild the `WebApp` project with the `CoreLib` project as a dependency.

After adding the reference, **you can now use the classes and methods defined in the `CoreLib` project from your `WebApp` project!**

### **Adding an XUnit project as well**

1. Open a terminal or command prompt window.
2. Navigate to the root directory of your `KnowYourAge` solution:

```bash
cd /path/to/KnowYourAge
```

3. Create a new xUnit test project called `CoreLibTests` using the .NET Core CLI:

```arduino
dotnet new xunit -n CoreLibTests
```

This will create a new folder called `CoreLibTests` with a new xUnit test project inside it.

4. Add the `CoreLibTests` project to the `KnowYourAge` solution:

```csharp
dotnet sln add CoreLibTests/CoreLibTests.csproj
```

5. Add a reference to the `CoreLib` project from the `CoreLibTests` project:

```csharp
dotnet add CoreLibTests/CoreLibTests.csproj reference CoreLib/CoreLib.csproj
```

This will add a project reference to the `CoreLib` project from the `CoreLibTests` project.

#### Note

Package reference to the `xunit` and `xunit.runner.visualstudio` packages in the `CoreLibTests.csproj` file is **added automatically**, in case ther CLI did not add it, you can add it manually as shown below:

```xml
<ItemGroup>
  <PackageReference Include="xunit" Version="2.4.1" />
  <PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
</ItemGroup>
```
