DEV Community

loading...

A Reference Application for Building Your First API with ASP.Net Core

pdelcogliano profile image Paul Delcogliano Updated on ・3 min read

A few months back I read this post by Brandon. 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. 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. 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

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
Enter fullscreen mode Exit fullscreen mode

Next, execute the Migration by running the update-database command:

Update-Database InitialMigration -Connection "Server=(localdb)\mssqllocaldb;Database=MuralAppData;Trusted_connection=true"
Enter fullscreen mode Exit fullscreen mode

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

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, 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Discussion (4)

pic
Editor guide
Collapse
lukaz profile image
Bello Luqman

Hi Paul

I am interested in learning with you.

Collapse
pdelcogliano profile image
Paul Delcogliano Author

Hi Bello, Glad to have you aboard. What experience do you have w/ ASP.Net?

Collapse
lukaz profile image
Bello Luqman

I am just starting to learn how to work with web API in ASP.NET

Thread Thread
pdelcogliano profile image
Paul Delcogliano Author

Hi Bello, I created a Slack workspace where we can discuss the project among other things. Here is an invite, Let’s work together in Slack! It’s a faster, simpler way to talk shop, share files, and get work done. Join here: join.slack.com/t/muralm-urldevslac...