Skip to content

Http route

Introduction

In a web application, HTTP routes are essential for enabling communication between the frontend and backend. HTTP requests allow clients (like a web browser) to communicate with the server by sending specific requests to defined endpoints. Each endpoint corresponds to a URL path associated with an action or resource in the application.

In AventusSharp, routes are defined as functions within specific classes, similar to controllers in an MVC pattern.

Defining Routes in AventusSharp

At application startup, once you register the RouterMiddleware, Aventus scans all classes that inherit from Router. For each of these classes, it examines the contained functions to automatically create the necessary HTTP routes.

By convention, routers are placed in the Routes namespace, but you are free to organize them however you wish. Aventus relies on the class type rather than location to identify routers.

Example: Defining a Basic Route

In the example below, MainRouter is a class that inherits from Router, defining a simple route that returns a message.

MainRouter.cs
using AventusSharp.Routes;
namespace Demo.Data.Routes
{
public class MainRouter : Router
{
public string Index() {
return "It's working";
}
}
}

In this setup:

  • The Index function is exposed as an HTTP route based on the name (Index = /).
  • When a request is made to this route, it returns the message β€œIt’s working.”

This approach streamlines route definition by automatically converting each method within Router-derived classes into accessible HTTP routes, simplifying communication between the frontend and backend.