간단한 API 테스트

This commit is contained in:
2025-10-15 02:27:39 +09:00
parent 6142899fed
commit 042f633603
3 changed files with 89 additions and 5 deletions

View File

@@ -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<IExceptionHandlerFeature>();
if (exceptionHandlerFeature != null)
{
var exception = exceptionHandlerFeature.Error;
// Do something here
}
});
} }
} }
} }

40
WarhoundConsole/Test.cs Normal file
View File

@@ -0,0 +1,40 @@
using Microsoft.AspNetCore.Mvc;
namespace WarhoundConsole
{
[ApiController]
[Route("api/test")]
public class Test : ControllerBase
{
[HttpGet("hello")]
public ActionResult<string> GetGreeting()
{
string message = "Hello!!!";
if (message.Length != 0)
{
//throw new Exception("Hello");
}
return Ok(message);
}
[HttpGet("data")]
public ActionResult<object> GetData()
{
var data = new { Id = 1, Name = "Sample Data", Timestamp = DateTime.UtcNow };
return Ok(data);
}
[HttpPost("json")]
public ActionResult<object> asd([FromBody] object obj)
{
//if (obj == null)
//{
// return BadRequest("Invalid data.");
//}
Console.Write(obj.ToString());
return Ok(new { a = 1, b = 2, c = "c" });
}
}
}

View File

@@ -9,6 +9,8 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\WarhoundLib\WarhoundLib.csproj" /> <ProjectReference Include="..\WarhoundLib\WarhoundLib.csproj" />
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="9.0.4" />
</ItemGroup> </ItemGroup>
</Project> </Project>