NET 6中的自托管Web API

原文:NET 6中的自托管Web API - 我爱学习网 (5axxw.com)

我需要向现有库中添加一个非常简单的Web API,以便Python可以与应用程序通信。简单的请求/JSON响应。这比最初想象的更具挑战性。我习惯了NodeJ,在那里像Express这样的库只需几行代码就可以做到这一点。

显然,web服务器需要集成到图书馆中。我不能依赖IIS或任何web服务器。

这些教程遍布网络:

https://github.com/jbogard/Docs/blob/master/aspnet/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api.md

Install: Microsoft.AspNet.WebApi.OwinSelfHost

Main

 static void Main(string[] args)
        {
            string baseAddress = "http://localhost:9000/";

            // Start OWIN host 
            using (WebApp.Start<Startup>(url: baseAddress))
            {
                // Create HttpCient and make a request to api/values 
                HttpClient client = new HttpClient();

                var response = client.GetAsync(baseAddress + "api/values").Result;

                Console.WriteLine(response);
                Console.WriteLine(response.Content.ReadAsStringAsync().Result);
                Console.ReadLine();
            }
        }

Startup

public class Startup
{
    // This code configures Web API. The Startup class is specified as a type
    // parameter in the WebApp.Start method.
    public void Configuration(IAppBuilder appBuilder)
    {
        // Configure Web API for self-host. 
        HttpConfiguration config = new HttpConfiguration();
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        appBuilder.UseWebApi(config);
    }
}

Controller

public class ValuesController : ApiController
    {
        // GET api/values 
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/values/5 
        public string Get(int id)
        {
            return "value";
        }

        // POST api/values 
        public void Post([FromBody] string value)
        {
        }

        // PUT api/values/5 
        public void Put(int id, [FromBody] string value)
        {
        }

        // DELETE api/values/5 
        public void Delete(int id)
        {
        }
    }

它看起来很简单,但在.NET6中不起作用。似乎存在兼容性问题。

我偶然发现threads,如下所示:

NET内核中的自托管OWIN

启动时Owin遇到NullReferenceException。Net Core 2.0-设置?

然而,对于如何在现有的.NET6库中部署简单的Web API,我正在努力找到一个实用的答案。建议的解决方法不适合我。

如有任何建议,我们将不胜感激?我宁愿去另一个图书馆吗?ASP.NET不是正确的工具吗?

 发布于 9 月前
 
✅ 最佳回答:
avatar

ASP.NETCore内置了默认的web服务器Kestrel,因此不需要设置OWIN。简单的设置可以这样看(UseKestrel在内部由WebApplication.CreateBuilder调用):

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", () => "Hello World!");

app.Run();

See also:

  • 托管并部署ASP.NET核心。
  • Servers
  • 使用ASP.NET核心共享框架
posted @   MaxBruce  阅读(615)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2021-05-07 WPF中的动画——(二)From/To/By 动画
点击右上角即可分享
微信分享提示