C# WebAPI [HttpGet("{cnt}"] pass argument

using Microsoft.AspNetCore.Mvc;
using WebApplication3.Models;
using WebApplication3.Services;

namespace WebApplication3.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class BookController : Controller
    {
        private BookService bkService;
        public BookController(BookService bkServiceValue)
        {
            bkService=bkServiceValue;
        }

        [HttpGet("{cnt}")]
        public ActionResult<List<Book>> GetBooks(int cnt=100000)
        {
            Console.WriteLine($"{DateTime.Now},cnt:{cnt}");
            return bkService.GetBooksData(cnt);
        }
    }
}

 

namespace WebApplication3.Models
{
    public class Book
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string ISBN { get; set; }
        public string Comment { get; set; }
        public string Content { get; set; }
        public string Summary { get; set; }
        public string Title { get; set; }
        public string Topic { get; set; }
    }
}
using WebApplication3.Models;

namespace WebApplication3.Services
{
    public class BookService
    {
        int idx = 0;
        private int GetIdx()
        {
            return Interlocked.Increment(ref idx);
        }

        public List<Book> GetBooksData(int cnt=10000)
        {
            List<Book> booksList=new List<Book>();
            for(int i=0;i<cnt;i++)
            {
                var a = GetIdx();
                booksList.Add(new Book()
                {
                    Id = a,
                    Name = $"Name_{a}",
                    ISBN = $"ISBN_{a}_{Guid.NewGuid():N}",
                    Comment=$"Comment_{a}",
                    Content=$"Content_{a}",
                    Summary=$"Summary_{a}",
                    Title=$"Title_{a}",
                    Topic=$"Topic_{a}"
                });
            }
            return booksList;
        }
    }
}
using Microsoft.AspNetCore.Mvc;
using WebApplication3.Models;
using WebApplication3.Services;

namespace WebApplication3.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class BookController : Controller
    {
        private BookService bkService;
        public BookController(BookService bkServiceValue)
        {
            bkService=bkServiceValue;
        }

        [HttpGet("{cnt}")]
        public ActionResult<List<Book>> GetBooks(int cnt=100000)
        {
            Console.WriteLine($"{DateTime.Now},cnt:{cnt}");
            return bkService.GetBooksData(cnt);
        }
    }
}

using WebApplication3.Services;

namespace WebApplication3
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.

            builder.Services.AddControllers();
            // Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
            builder.Services.AddOpenApi();

            builder.Services.AddSingleton<BookService>();
            var app = builder.Build();

            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment())
            {
                app.MapOpenApi();
            }

            app.UseHttpsRedirection();

            app.UseAuthorization();


            app.MapControllers();

            app.Run();
        }
    }
}

 

 

 

image

 

image

 

 

 

image

 

posted @ 2026-03-17 23:08  FredGrit  阅读(1)  评论(0)    收藏  举报