ASP.NET

ASP.NET is a web application framework built on top of the .NET framework. It is used to build dynamic web applications and web services using languages such as C# or Visual Basic .NET. ASP.NET includes features such as web forms, MVC, web API, and SignalR for building different types of web applications.

Razor Pages

Razor Pages is a less complex than MVC.

However, instead of the Models, Views, and Controllers that make MVC, Razor Pages have 2 parts:

  • Razor Page (Similar to View)
  • Page Model (Contains all the non-View code)

Everything that can be done with MVC can also be done with Razor Pages.

Create and run your first ASP.NET Razor Pages project:

This component was made by Stratis Dermanoutsos. The code can be found here.

What the default generated project contains:

  • The Program.cs and Startup.cs files

    These files set up the web server and ASP.NET Core pipeline.

  • The wwwroot directory

    It contains static assets like CSS, JavaScript and image files. Files in wwwroot will be served as static content, and can be bundled and minified automatically.

  • The appsettings.json file

    It contains configuration settings ASP.NET Core will load on startup. You can use this to store database connection strings or other things that you don’t want to hard-code.

  • The launchSettings.json property file

    This is basically the configuration file for when the project is run.

  • The Pages directory

    This folder contains all of our pages (*.cshtml) files and their respective Models (*.cshtml.cs).

Routing in Razor Pages

Create a simple Page to display Cars

  • Add this to the Pages/Shared/_Layout.cshtml file, after the other navbar links

    This component was made by Stratis Dermanoutsos. The code can be found here.
  • Create a CarList directory inside the Pages one and now we’ll make the following files there:

    • Index.cshtml

      This component was made by Stratis Dermanoutsos. The code can be found here.
    • Index.cshtml.cs

      This component was made by Stratis Dermanoutsos. The code can be found here.

    With the logic existing above, it is easy to write Razor Pages to Create and Edit cars in the database.

MVC

Create and run your first ASP.NET MVC project:

This component was made by Stratis Dermanoutsos. The code can be found here.

What the default generated project contains:

  • The Program.cs and Startup.cs files

    These files set up the web server and ASP.NET Core pipeline.

  • The Models, Views, and Controllers directories

    This project type is based on the MVC architecture.

  • The wwwroot directory

    It contains static assets like CSS, JavaScript and image files. Files in wwwroot will be served as static content, and can be bundled and minified automatically.

  • The appsettings.json file

    It contains configuration settings ASP.NET Core will load on startup. You can use this to store database connection strings or other things that you don’t want to hard-code. More specifics:

  • Routes that are handled by Controllers are called actions, and are represented by methods in the Controller class. For example, the HomeController includes three action methods (Index, About, and Contact) which are mapped by ASP.NET Core to these route URLs:

    |- localhost:5000/Home->          Index()
    |- localhost:5000/Home/About->    About()
    |- localhost:5000/Home/Contact -> Contact()
    
  • Views in ASP.NET Core are built using the Razor templating language, which combines HTML and C#.

    Most view code is just HTML, with the occasional C# statement added in to pull data out of the View Model and turn it into text or HTML.

    The C# statements are prefixed with the @ symbol.

  • The base HTML file, which contains the <body> tag, is called the layout View.

    It can be found at

    |- Views/Shared/_Layout.cshtml
    
  • As mentioned above, to edit the CSS or anything static, you must go to

    |- wwwroot/css/site.css
    

Create simple Model, View and Controller to display Cars

  • Add this to the Views/Shared/_Layout.cshtml file, after the other navbar links

    This component was made by Stratis Dermanoutsos. The code can be found here.
  • Create a CarsController.cs with the following code

    This component was made by Stratis Dermanoutsos. The code can be found here.
  • Create a Cars directory inside the Views one and now we’ll make the following files there.

Web API

Web API (Application Programming Interface) is a set of subroutine definitions with the scope of managing data between clients and servers. Building APIs over HTTP protocol allows third-party apps to interact with a server thanks to the application protocol.

Create and run your first ASP.NET API project:

This component was made by Stratis Dermanoutsos. The code can be found here.

What the default generated project contains:

  • The Program.cs and Startup.cs files

    These files set up the web server and ASP.NET Core pipeline.

  • A WeatherForecast class

    This is our Model which describes the data.

  • A Controllers directory holding the WeatherForecastController

    This Controller is responsible for generating data based on our Model and returning it in JSON every time we visit the link below.

Now, if you visit https://localhost:5001/weatherforecast, you’ll be presented with data in JSON format.

For example,

This component was made by Stratis Dermanoutsos. The code can be found here.

Useful features

Quick Route Short Circuit

In .NET 8, C# got an amazing new feature.

Let’s say we have an API with several endpoints, and we want to add a new one that just returns "Hello World".

We can do this:

This component was made by Stratis Dermanoutsos. The code can be found here.

You can notice pretty quickly, that we’ve added a simple middleware which simply logs the time it took to process the request. In a real-world scenario, this could be a lot more complex, and could even be a third-party library that takes up resources from our server.

This new feature allows us to completely bypass all middleware and return a response immediately. To use this, simply add .ShortCircuit(); at the end of your route declaration.

In this case, we get something like this:

This component was made by Stratis Dermanoutsos. The code can be found here.

Now, everytime we call the / endpoint, we get a response immediately, without any middleware being called. This practice can, depending on the situation, save a lot of resources.

It’s also useful when you want to send a quick 404 for frequently called endpoints that don’t exist in your API.

This component was made by Stratis Dermanoutsos. The code can be found here.

By using this, everytime a crawler or a bot tries to access an endpoint that doesn’t exist, we can send a quick 404 response without having to go through any of the middleware.

The end result of the above Program.cs file is:

This component was made by Stratis Dermanoutsos. The code can be found here.

Note: This feature is only available in .NET 8 and above.

Note: This feature is NOT available in controllers currently and we don’t know if it’ll ever be.

Resources