From 042f63360343f30115700518d5210b43847dff7d Mon Sep 17 00:00:00 2001 From: tymmkang Date: Wed, 15 Oct 2025 02:27:39 +0900 Subject: [PATCH] =?UTF-8?q?=EA=B0=84=EB=8B=A8=ED=95=9C=20API=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WarhoundConsole/Program.cs | 50 +++++++++++++++++++++++--- WarhoundConsole/Test.cs | 40 +++++++++++++++++++++ WarhoundConsole/WarhoundConsole.csproj | 4 ++- 3 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 WarhoundConsole/Test.cs diff --git a/WarhoundConsole/Program.cs b/WarhoundConsole/Program.cs index 9930818..e2c2a11 100644 --- a/WarhoundConsole/Program.cs +++ b/WarhoundConsole/Program.cs @@ -1,10 +1,52 @@ -namespace WarhoundConsole +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Diagnostics; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.DependencyInjection; + +namespace WarhoundConsole { - internal class Program + public class Program { - static void Main(string[] args) + public static void Main(string[] args) { - Console.WriteLine("Hello, World!"); + try + { + MainInternal(args); + } + catch (Exception e) + { + Console.WriteLine(e); + } + } + + private static void MainInternal(string[] args) + { + var builder = WebApplication.CreateBuilder(args); + builder.WebHost.UseUrls("http://localhost:80"); + builder.Services.AddControllers(); + builder.Services.AddEndpointsApiExplorer(); + builder.Services.AddSwaggerGen(); + + var app = builder.Build(); + app.UseSwagger(); + app.UseSwaggerUI(); + app.MapControllers(); + app.UseExceptionHandler(ExceptionHandler); + app.Run(); + } + + private static void ExceptionHandler(IApplicationBuilder applicationBuilder) + { + applicationBuilder.Run(async context => + { + var exceptionHandlerFeature = context.Features.Get(); + if (exceptionHandlerFeature != null) + { + var exception = exceptionHandlerFeature.Error; + + // Do something here + } + }); } } } diff --git a/WarhoundConsole/Test.cs b/WarhoundConsole/Test.cs new file mode 100644 index 0000000..029a799 --- /dev/null +++ b/WarhoundConsole/Test.cs @@ -0,0 +1,40 @@ +using Microsoft.AspNetCore.Mvc; + +namespace WarhoundConsole +{ + [ApiController] + [Route("api/test")] + public class Test : ControllerBase + { + [HttpGet("hello")] + public ActionResult GetGreeting() + { + string message = "Hello!!!"; + if (message.Length != 0) + { + //throw new Exception("Hello"); + } + return Ok(message); + } + + [HttpGet("data")] + public ActionResult GetData() + { + var data = new { Id = 1, Name = "Sample Data", Timestamp = DateTime.UtcNow }; + return Ok(data); + } + + [HttpPost("json")] + public ActionResult asd([FromBody] object obj) + { + //if (obj == null) + //{ + // return BadRequest("Invalid data."); + //} + + Console.Write(obj.ToString()); + + return Ok(new { a = 1, b = 2, c = "c" }); + } + } +} diff --git a/WarhoundConsole/WarhoundConsole.csproj b/WarhoundConsole/WarhoundConsole.csproj index c9220cb..3429823 100644 --- a/WarhoundConsole/WarhoundConsole.csproj +++ b/WarhoundConsole/WarhoundConsole.csproj @@ -8,7 +8,9 @@ - + + +