我需要向现有库中添加一个非常简单的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

安装: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中不起作用.似乎存在兼容性问题.

我偶然发现了如下线索:

Self Hosting OWIN in .NET Core

NullReferenceException experienced with Owin on Startup .Net Core 2.0 - Settings?

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

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

推荐答案

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

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

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

app.Run();

另请参见:

Csharp相关问答推荐

需要更改哪些内容才能修复被覆盖的财产中的无效警告CS 8765?

无法使用ternal- .net修复可空警告

如何使嵌套for-loop更高效?

错误NU 1301:无法加载源的服务索引

C#将参数传递给具有变化引用的变量

FileStream. FlushAsync()是否确保文件被写入磁盘?

在C#WinUI中,一个关于System的崩溃."由于未知原因导致执行不例外"

==和Tuple对象的相等<>

为什么任务需要在内部使用ManualResetEventSlim?

在命名管道上使用GRPC ASP.NET核心时如何配置命名管道权限

由于POST中的应用程序/JWT,出现不支持的内容类型异常

具有可空类型的C#NOTNULL约束具有意外行为

记录类型';==运算符是否与实现IEquatable<;T&>;的类中的';equals&>方法执行等价比较?

数据库.Migrate在对接容器重启时失败

如何实现有条件的自定义Json转换器隐藏属性

单元测试类型为HttpClient with Microsoft.Extensions.Http.Resilience

如何在mediatr命令中访问HttpContext而不安装弃用的nuget包

WPF:如何从DatagridHeader的内容模板绑定到词典项

使DefaultIfEmpty返回空

最小API定义的Swagger标头参数