Skip to content

Instantly share code, notes, and snippets.

@smart-onion
Created August 20, 2025 08:53
Show Gist options
  • Select an option

  • Save smart-onion/34632adb95819863f7347d717db791ca to your computer and use it in GitHub Desktop.

Select an option

Save smart-onion/34632adb95819863f7347d717db791ca to your computer and use it in GitHub Desktop.
ASP.NET HW5 Minimal API
using WebApplication1.Middlewares;
using WebApplication1.Model;
using WebApplication1.Services;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("upload/image", async (context) =>
{
context.Response.ContentType = "text/html";
context.Response.StatusCode = 200;
await context.Response.SendFileAsync(Path.Combine(app.Environment.WebRootPath, "index.html"));
});
app.MapPost("upload/image", async (HttpContext context, IFormFile file) =>
{
try
{
if (file.Length > 0 || !string.IsNullOrEmpty(file.FileName))
{
var path = Path.Combine(app.Environment.WebRootPath, Path.GetRandomFileName() + "-"+ file.FileName);
using (var stream = new FileStream(path, FileMode.Create))
{
await file.CopyToAsync(stream);
}
await context.Response.WriteAsync(path);
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}).DisableAntiforgery();
app.Run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment