Created
August 20, 2025 08:53
-
-
Save smart-onion/34632adb95819863f7347d717db791ca to your computer and use it in GitHub Desktop.
ASP.NET HW5 Minimal API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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