ASP.NET Core  

ASP.NET Core with C#: A Complete Introduction and Practical Example

Introduction

ASP.NET Core is a modern, open-source, cross-platform framework developed by Microsoft for building web applications, REST APIs, and cloud-based services. It is designed to be fast, lightweight, and highly scalable, making it an excellent choice for enterprise and personal projects alike.

With ASP.NET Core, developers can create applications that run on Windows, Linux, and macOS using the same codebase. Combined with the power of C#, it provides a productive environment for developing secure and high-performance web applications.

Why Choose ASP.NET Core?

Some key advantages of ASP.NET Core include:

  • Cross-platform development

  • High performance and scalability

  • Built-in dependency injection

  • Middleware-based request pipeline

  • Integrated security features

  • Easy deployment to cloud platforms such as Azure

  • Support for RESTful APIs and microservices

Project Structure

A typical ASP.NET Core Web API project contains files and folders such as:

  • Program.cs

  • appsettings.json

  • Controllers/

  • Models/

  • Services/

  • Properties/

The Program.cs file configures the application and registers services.

Creating a Simple Web API

Let's create a simple API that returns a welcome message.

Program.cs

var builder = WebApplication.CreateBuilder(args);

// Register controller services
builder.Services.AddControllers();

var app = builder.Build();

// Configure middleware
app.UseHttpsRedirection();

app.MapControllers();

app.Run();

Creating a Controller

Create a file named HomeController.cs inside the Controllers folder.

using Microsoft.AspNetCore.Mvc;

namespace DemoApi.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class HomeController : ControllerBase
    {
        [HttpGet]
        public IActionResult Get()
        {
            return Ok("Welcome to ASP.NET Core!");
        }
    }
}

When the application runs and you browse to:

https://localhost:5001/api/home

The response will be:

Welcome to ASP.NET Core!

Creating a Model

Suppose we want to represent a product.

namespace DemoApi.Models
{
    public class Product
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public decimal Price { get; set; }
    }
}

Returning Objects from an API

using Microsoft.AspNetCore.Mvc;
using DemoApi.Models;

namespace DemoApi.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class ProductController : ControllerBase
    {
        [HttpGet]
        public IActionResult GetProduct()
        {
            Product product = new Product
            {
                Id = 1,
                Name = "Laptop",
                Price = 999.99m
            };

            return Ok(product);
        }
    }
}

The API returns JSON:

{
  "id": 1,
  "name": "Laptop",
  "price": 999.99
}

Handling POST Requests

You can also receive data from clients.

[HttpPost]
public IActionResult CreateProduct(Product product)
{
    return Ok(new
    {
        Message = "Product Created Successfully",
        Data = product
    });
}

Sample Request Body

{
    "id": 2,
    "name": "Keyboard",
    "price": 45.50
}

Dependency Injection Example

Create a Service Interface

public interface IMessageService
{
    string GetMessage();
}

Implement the Service

public class MessageService : IMessageService
{
    public string GetMessage()
    {
        return "Hello from Dependency Injection!";
    }
}

Register the Service in Program.cs

builder.Services.AddScoped<IMessageService, MessageService>();

Use the Service Inside a Controller

private readonly IMessageService _messageService;

public HomeController(IMessageService messageService)
{
    _messageService = messageService;
}

[HttpGet("message")]
public IActionResult GetMessage()
{
    return Ok(_messageService.GetMessage());
}

Benefits of Using ASP.NET Core

  • Excellent performance compared to many traditional web frameworks.

  • Built-in support for REST APIs.

  • Clean architecture using dependency injection.

  • Easy integration with Entity Framework Core.

  • Cross-platform compatibility.

  • Active community and long-term Microsoft support.

Conclusion

ASP.NET Core has become one of the leading frameworks for developing modern web applications and APIs. Its modular architecture, excellent performance, and seamless integration with C# make it suitable for projects ranging from small business applications to enterprise-scale systems.

By understanding controllers, models, dependency injection, and routing, developers can quickly begin building scalable and maintainable applications using ASP.NET Core.

Summary

ASP.NET Core is a fast, modern, and cross-platform framework for building web applications and REST APIs. It offers features such as dependency injection, middleware, built-in security, and cloud readiness. By learning the fundamentals of controllers, models, routing, and dependency injection, developers can efficiently create scalable, maintainable, and high-performance applications for a variety of business and enterprise needs.