I Install Microsoft.AspNetCore.Mvc
and Microsoft.AspNetCore
package can't use
I look at this WebApplication.CreateBuilder Method Doc find need Microsoft.AspNetCore.dll, but I can't use this.
I hope it helps others.
Install Microsoft.AspNetCore.App package
dotnet add package Microsoft.AspNetCore.App --version 2.2.8
the Minimal API:
Microsoft.AspNetCore.Builder.WebApplication app = Microsoft.AspNetCore.Builder.WebApplication.Create(new string[] { });
app.MapGet("/", () => "Hello World!");
app.RunAsync();
Ok, now you can open http:localhost:5000
to look Hello World!
Because few people will use parameters in WinForm, string[] args
is omitted here, and new string[] { }
is used instead
If you want to ues controller, like ASP.NET Core, then you can use this:
var builder = Microsoft.AspNetCore.Builder.WebApplication.CreateBuilder(new string[]{});
builder.Services.AddControllers();
var app = builder.Build();
app.MapControllers();
app.RunAsync();
HomeController.cs
using Microsoft.AspNetCore.Mvc;
namespace RuyutWinFormsApi;
[Route("api/[controller]")]
[ApiController]
public class HomeController : ControllerBase
{
[HttpGet]
public IActionResult Get()
{
return Ok("This is a test api");
}
}
you can open http://localhost:5000/api/home
to look This is a test api
BTY, if you want to change port, you can add this:
app.Urls.Add("http://0.0.0.0:8080");