DEV Community

ben
ben

Posted on

Build Web Applications Just Like Calling a Method

Many .NET developers have extensive experience in desktop and device development, yet web development is not their primary focus.
PicoServer enables you to quickly add web capabilities to your applications—no need to be a web expert—as naturally as calling the familiar methods you know.

You can set up your first WebAPI service in just 1 minute:

1. Install the PicoServer Library

  1. Install PicoServer via the NuGet Package Manager: PicoServer
    30c11767350649.png

  2. Install via Command Line

# .NET CLI
dotnet add package PicoServer
# Package Manager Console
NuGet\Install-Package PicoServer
Enter fullscreen mode Exit fullscreen mode

2. C# Quick Start Example

using System.Net;
using PicoServer;

namespace FastTestNamespace
{
    public static class FastTest
    {
        private static readonly WebAPIServer MyAPI = new WebAPIServer();

        public static void Main()
        {
            MyAPI.AddRoute("/", Hello); // Map the root route
            MyAPI.StartServer(); // Start the server (default port: 8090)
            Console.WriteLine("PicoServer started successfully! Access address: http://127.0.0.1:8090");
            Console.ReadKey();
        }

        // Asynchronous request handling method
        private static async Task Hello(HttpListenerRequest request, HttpListenerResponse response)
        {
            await response.WriteAsync("""{"code":1,"msg":"Hello WebAPI"}""");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

3. VB.NET Quick Start Example

Imports System.Net
Imports PicoServer

Module FastTest
    Private ReadOnly MyAPI As New WebAPIServer
    Sub Main()
        MyAPI.AddRoute("/", AddressOf hello) 'Add route mapping
        MyAPI.StartServer() 'Start the WebAPI server (default port: 8090; modify port by passing a parameter)
        Console.WriteLine("Access address: http://127.0.0.1:8090")
        Console.ReadKey()
    End Sub

    Private Async Function hello(request As HttpListenerRequest, response As HttpListenerResponse) As Task
        Await response.WriteAsync(<t>{"code":1,"msg":"Hello WebAPI"}</t>.Value)
    End Function
End Module
Enter fullscreen mode Exit fullscreen mode

4. Run and Test

After launching the project, visit http://127.0.0.1:8090 and you will receive the following response:

{"code":1,"msg":"Hello WebAPI"}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)