ASP.Net Core WebAPI plays as LAN server, other device can access data in the LAN

//D:\C\WebApplication3\Program.cs

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

            builder.WebHost.UseKestrel(x =>
            {
                x.ListenAnyIP(5000);
                x.ListenAnyIP(5001, o =>
                {
                    o.UseHttps();
                });
            });
            // Add services to the container.

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

            var app = builder.Build();

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

            //app.UseHttpsRedirection();

            app.UseAuthorization();


            app.MapControllers();

            app.Run();
        }
    }
}


//D:\C\WebApplication3\Controllers\BooksController.cs
using Microsoft.AspNetCore.Mvc;
using WebApplication3.Models;

namespace WebApplication3.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class BooksController : ControllerBase
    {
        private static string[] enumNames = Enum.GetNames(typeof(BookCategory));
        private static int enumLen = enumNames.Length;
        private static long id = 1;

        private static (long, long) GetStartEnd(int cnt = 1000000)
        {
            long end = Interlocked.Add(ref id, cnt);
            long start = end - cnt;
            return (start, end);
        }

        [HttpGet("getbooks/{cnt}")]
        public List<Book> GetBooks(int cnt = 1000000)
        {
            List<Book> bksList = new List<Book>(cnt);
            var (start, end) = GetStartEnd(cnt);
            for (long i = start; i < end; i++)
            {
                bksList.Add(new Book()
                {
                    Id = i,
                    Name = $"Name_{i}",
                    ISBN = $"ISBN_{i}_{Guid.NewGuid():N}",
                    CategoryName = $"{enumNames[i % enumLen]}",
                    Comment = $"Comment_{i}",
                    Content = $"Content_{i}",
                    Author = $"Author_{i}",
                    Summary = $"Summary_{i}",
                    Title = $"Title_{i}",
                    Topic = $"Topic_{i}"
                });
            }
            Console.WriteLine($"In WebAPI,{DateTime.Now},FirstId:{bksList.FirstOrDefault()?.Id}," +
                $"LastId:{bksList.LastOrDefault()?.Id}");
            return bksList;
        }
    }
}


//D:\C\WebApplication3\Models\Book.cs
namespace WebApplication3.Models
{
    public class Book
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public string ISBN { get; set; }
        public string CategoryName { get; set; }
        public string Comment { get; set; }
        public string Author { get; set; }
        public string Content { get; set; }
        public string Summary { get; set; }
        public string Title { get; set; }
        public string Topic { get; set; }
    }

    enum BookCategory
    {
        Science,
        Technology,
        Engineering,
        Math
    }
}

 

 

 

https://192.168.1.142:5001/api/books/getbooks/1000

 

 

 

 

image

 

http://192.168.1.142:5000/api/books/getbooks/1000

image

 

 

af94af6497389c379f18952ec2b015e

 

 

 

 

 

 

e9910815a9f95930598dc629e6e3202

 

 

 

image

 

posted @ 2026-09-28 20:01  FredGrit  阅读(13)  评论(0)    收藏  举报