Train Booking Project Idea with .NET 8 | ASP NET MVC Core , C# | Train Ticket booking Website

Train Booking System using .NET 8

Train Booking System using .NET 8

1. Introduction

A Train Booking System is a web-based application that allows users to book train tickets, select seats, and make payments online. In this guide, we build a .NET 8-based system that includes authentication, scheduling, booking, and payment processing.

2. Prerequisites

  • Basic knowledge of:
    • C# (for backend development)
    • ASP.NET Core MVC (for frontend & API development)
    • Entity Framework Core (for database management)
  • Tools required:
    • Visual Studio 2022
    • .NET 8 SDK
    • SQL Server
    • Postman
  • Libraries used:
    • Entity Framework Core
    • ASP.NET Core Identity
    • Stripe.net

3. System Requirements

User Features

  • User Authentication (Login, Register, Logout)
  • View Train Schedules
  • Seat Selection & Booking
  • Make Payments (Using Stripe/PayPal)
  • Ticket Cancellation & Refunds

Admin Features

  • Manage Train Schedules
  • View and Manage Bookings

4. Architecture Overview

The system follows a Client-Server Model:

  • Client (Frontend): ASP.NET Core MVC, Razor Pages, HTML, CSS, JavaScript
  • Server (Backend): ASP.NET Core Web API
  • Database: SQL Server
  • Payment Gateway: Stripe / PayPal

5. Setting Up the Project

dotnet new webapi -n TrainBookingSystem
cd TrainBookingSystem

dotnet add package Microsoft.EntityFrameworkCore.SqlServer

6. Building the Backend

User Authentication

public class ApplicationUser : IdentityUser { }

public class AppDbContext : IdentityDbContext
{
    public DbSet<Train> Trains { get; set; }
    public DbSet<Booking> Bookings { get; set; }
    public DbSet<Payment> Payments { get; set; }

    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
}

Train Schedule Management API

[HttpPost]
public async Task AddTrain([FromBody] Train train)
{
    _context.Trains.Add(train);
    await _context.SaveChangesAsync();
    return Ok(new { Message = "Train added successfully!" });
}

7. Developing the Frontend

Train List (Index.cshtml)

<table>
    <tr><th>Train Name</th><th>Route</th><th>Action</th></tr>
    @foreach (var train in Model)
    {
        <tr>
            <td>@train.Name</td>
            <td>@train.Route</td>
            <td><a href="/Booking/Book/@train.TrainID">Book Now</a></td>
        </tr>
    }
</table>

8. Implementing Payment Processing

Stripe Payment Integration

[HttpPost("pay")]
public async Task ProcessPayment([FromBody] PaymentRequest request)
{
    var options = new ChargeCreateOptions
    {
        Amount = request.Amount * 100,
        Currency = "usd",
        Source = request.Token
    };

    var service = new ChargeService();
    Charge charge = service.Create(options);

    return Ok(new { Message = "Payment successful!" });
}

9. Testing and Debugging

  • Unit Testing using xUnit/NUnit
  • API Testing with Postman
  • Debugging using Visual Studio Debugger

10. Deployment

Deploying to Azure

  • Use Azure App Service for hosting
  • Set up CI/CD pipelines with GitHub Actions

11. Conclusion

🎉 Congratulations! You have successfully built a Train Booking System using .NET 8.

  • Setting up a .NET Web API project
  • Developing APIs for train schedules, booking, and payments
  • Implementing authentication and authorization
  • Using Stripe API for online payments
  • Deploying to Azure & AWS