C# Developers: YouTube-Style Numeric IDs Made Easy in ASP.Net Core | #csharptutorial - SkillBakery Studios

Breaking

Post Top Ad

Post Top Ad

Friday, July 17, 2026

C# Developers: YouTube-Style Numeric IDs Made Easy in ASP.Net Core | #csharptutorial

C# Developers: YouTube-Style Numeric IDs Made Easy in ASP.Net Core | #csharptutorial

Screenshot from the tutorial
Screenshot from the tutorial

C# Developers: YouTube-Style Numeric IDs Made Easy in ASP.Net Core

In this tutorial, we will explore a common requirement in web applications: creating user-friendly, YouTube-style numeric IDs. This is especially useful for developers working with ASP.NET Core applications where clean URLs and easy-to-manage identifiers are essential. Whether you're building an API or a full-fledged web application, implementing numeric IDs can enhance the user experience.

Understanding YouTube-Style Numeric IDs

YouTube-style numeric IDs are generally simple integers that are easy to read and remember. For example, instead of using complex strings or GUIDs, a video might be identified simply by an integer like 123456. This approach can improve the usability of your URLs and make it easier for users to share resources.

Setting Up Your ASP.NET Core Project

Before we dive into the implementation, ensure you have the latest version of the .NET SDK installed. You can create a new ASP.NET Core project using the following command:

dotnet new webapi -n YouTubeStyleIDs
cd YouTubeStyleIDs

This will create a new Web API project named YouTubeStyleIDs.

Creating the Model

For our example, we will create a simple model representing a video. This model will include properties for the ID and the title of the video.

Video Model

Create a new class called Video.cs in the Models folder:

namespace YouTubeStyleIDs.Models
{
    public class Video
    {
        public int Id { get; set; }
        public string Title { get; set; }
    }
}

This simple model includes an integer Id and a Title for the video.

Setting Up the Controller

Next, we will create a controller to manage our videos. The controller will include actions for creating and retrieving videos using their numeric IDs.

Video Controller

Create a new controller named VideosController.cs in the Controllers folder:

using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using YouTubeStyleIDs.Models;

namespace YouTubeStyleIDs.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class VideosController : ControllerBase
    {
        private static List<Video> videos = new List<Video>();

        [HttpGet]
        public ActionResult<IEnumerable<Video>> GetVideos()
        {
            return Ok(videos);
        }

        [HttpGet("{id}")]
        public ActionResult<Video> GetVideo(int id)
        {
            var video = videos.FirstOrDefault(v => v.Id == id);
            if (video == null)
            {
                return NotFound();
            }
            return Ok(video);
        }

        [HttpPost]
        public ActionResult<Video> CreateVideo([FromBody] Video video)
        {
            video.Id = videos.Count > 0 ? videos.Max(v => v.Id) + 1 : 1; // Auto-increment ID
            videos.Add(video);
            return CreatedAtAction(nameof(GetVideo), new { id = video.Id }, video);
        }
    }
}

Explanation of the Code

  1. Static List: We use a static list to store our videos temporarily. In a real-world application, this would typically be replaced with a database.
  2. GetVideos: This action retrieves all videos.
  3. GetVideo: This action retrieves a single video by its numeric ID. If the video does not exist, it returns a 404 Not Found response.
  4. CreateVideo: This action creates a new video and auto-increments the ID based on the existing videos.

Testing the API

Now that we have set up our controller, we can test our API using tools like Postman or curl.

Creating a New Video

To create a new video, send a POST request to http://localhost:5000/api/videos with the following JSON body:

{
    "title": "My First Video"
}

Retrieving Videos

To retrieve all videos, send a GET request to http://localhost:5000/api/videos.

To retrieve a specific video, send a GET request to http://localhost:5000/api/videos/1.

Conclusion

In this tutorial, we demonstrated how to implement YouTube-style numeric IDs in an ASP.NET Core application. By creating a simple model and controller, we were able to manage video resources with clean and user-friendly numeric identifiers.

This pattern can be extended to various applications, enhancing usability and maintainability. As you continue to develop your ASP.NET Core applications, consider how numeric IDs can improve the overall user experience.

Feel free to explore further by integrating a database, adding authentication, or expanding the functionality of your video service! Happy coding!

Another screenshot from the tutorial
Another view from the tutorial

Connect with SkillBakery Studios

Explore more tutorials, tools, and resources:

Posted by SkillBakery Studios

No comments:

Post a Comment

Post Top Ad