A Reference Application for Building Your First API with ASP.Net Core
Tags: beginners, dotnet, api, csharp
Published: 2021-02-09 18:14:06

A few months back I read this [post] (https://dev.to/brandonkylebailey/want-recruiters-attention-build-this-project-in-5-minutes-to-your-portfolio-1l6h) by [Brandon] (https://dev.to/brandonkylebailey). Brandon discussed how to get up to speed on the MERN stack in five minutes. His post inspired me to recreate his sample application using the .NET stack. My interpretation of his project is available on [GitHub](https://github.com/pdelcogliano/m-url). The project is named "m-url", which I am calling "Mural".

I started Mural as an educational project and reference application. I intend to use it to learn new technologies and refer to it from time-to-time. 

Mural is a service that creates shortened URLs. Think of it as a much simpler version of [TinyURL](https://www.tinyurl.com). Like TinyURL, Mural creates short links to lengthy, deep URLs. Mural is built on top of .NET Framework Core 3.1. It uses Entity Framework Core to interact with a local SQL database. Mural is logically separated into several projects, shown in the screenshot below.

![Image 1: m-url project structure](https://res.cloudinary.com/redfive/image/upload/v1612731890/Dev.to/m-url/m-urlProjectStructure_e8vziw.png)

_API_ is the main project. It follows RESTful practices and contains the endpoints for adding and retrieving data. The _Data_ project contains the repository which connects to the database. The _Data.Migrations_ project contains code that creates the SQL Server LocalDb database. The project named _Services_ contains the code for creating short "slugs" to represent the shortened URLs.

### Getting Mural Up and Running

After downloading the code from GitHub, you will need to perform two tasks to run Mural locally. The first is to open the solution file named "m-url.sln" in Visual Studio and confirm that the _API_ project is set as the startup project.

The second task is to create the SQL Server LocalDb database on your workstation. You'll do this using a Migration. A Migration is an Entity Framework feature that creates scripts from your application's data model. I won't go into details about Migrations now, just know you need to run a Migration to setup the Mural database.

To run the Migration you'll need to install a NuGet package. If you are running Visual Studio and using Powershell, like I am, you need the _Microsoft.EntityFrameworkCore.Tools_ package. Alternatively, if you are using VS Code or the dotnet CLI, you need to install the _Microsoft.EntityFrameworkCore.Tools.DotNet_ package. To install the tools for Visual Studio, execute the following command from the Package Manager Console:
```
Install-Package Microsoft.EntityFrameworkCore.Tools
```
Next, execute the Migration by running the update-database command:
```
Update-Database InitialMigration -Connection "Server=(localdb)\mssqllocaldb;Database=MuralAppData;Trusted_connection=true"
```
To confirm the Migration is successful, open the SQL Server Object Explorer window in Visual Studio and drill down into the server named _(localdb)\MSSQLLocalDB_. There you will see the local database named MuralAppData with a table named "slugs", as shown in the following screenshot.
 
![Image2: SQL Server LocalDb](https://res.cloudinary.com/redfive/image/upload/v1612884457/Dev.to/m-url/LocalMuralAppDatabase_p5leb8.png)

Now you can run the application. Create a Mural slug by posting a request to the API. The following HTTP Post request, made using [curl](https://github.com/curl/curl), illustrates how to create a Mural slug. It assumes the API is running on local port 44333. If you are running on a different port, or are not using SSL, be sure to change the port and protocol in the script below:
```
curl --header "Content-Type: application/json" `
--request POST `
--data '{\"url\":\"http://www.test.com/long/resource/path\"}' `
https://localhost:44333/api/slugs
```
You can retrieve the newly created slug from the API by issuing the following curl command:
```
curl --header "Content-Type: application/json" `
--request GET `
https://localhost:44333/api/slugs
```
As my bio states, I am in the process of refreshing my skillset. My current focus is on Azure, .NET Core, Entity Framework, and Git. I plan to use Mural for hands-on learning of these new technologies. This is the introduction to a series of posts I plan on creating. Over the next several months, I will be adding advanced data techniques like paging, sorting, and searching. I also plan on securing the API, hosting Mural on Azure, incorporating Azure Functions, and completing the Web UI. If you are interested in joining me on this learning path, reach out and we can learn together.