Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对于C#/ASP.NET核心,ExpressJS的等价物是什么?_C#_Asp.net Core - Fatal编程技术网

对于C#/ASP.NET核心,ExpressJS的等价物是什么?

对于C#/ASP.NET核心,ExpressJS的等价物是什么?,c#,asp.net-core,C#,Asp.net Core,在ExpressJs中,您可以快速设置web服务器,让它侦听请求,并在线注册处理程序 var express = require('express') var app = express() // respond with "hello world" when a GET request is made to the homepage app.get('/', function (req, res) { res.send('hello world

在ExpressJs中,您可以快速设置web服务器,让它侦听请求,并在线注册处理程序

    var express = require('express')
    var app = express()

    // respond with "hello world" when a GET request is made to the homepage
    app.get('/', function (req, res) {
        res.send('hello world')
    })
是否可以在ASP.NET核心中使用类似的东西并使用中间件来实现这一点

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.Get("/api/v2/", () =>
        {
            return "Hello world";
        });


        //configure more services...
    }
能够在同一过程中启动多个web侦听器也很方便:

using (var server = new WebServer(url))
{
    server.Get("/path", () => { return "Hello world" });
    server.Get("/path2", () => { return "Hello world2" });
    server.RunAsync();
}

会很方便。

你可以用这样的东西。不像Express中的1-liner,但对于将同一路线的不同方法分组到1个区块中,仍然可能非常有用

app.Map("/test", (builder) =>
        {
            builder.MapWhen(context => context.Request.Method == "GET", (app2) =>
            {
                app2.Run(async ctx =>
                {
                    await ctx.Response.WriteAsync("I have been reached.");
                });
            });
        });

我试图创建一个允许node.js在.NETCore中使用精确语法的项目,我意识到它们各自的语法适用于各自的环境。我建议您了解一下.NETCore的WebAPI设置,您可以检查如何在利用框架组件的同时拥有端点

但是,要使用您在第二段中显示的确切代码示例,您需要在项目中定义如下类:

public static class AppExtensions
{

    public static IApplicationBuilder Get(this IApplicationBuilder app,
                                            string path, Func<string> run) 
        => app.Use(async (context, next) =>
            {
                if (context.Request.Path == path)
                    await context.Response.WriteAsync(run());
                else await next();
            });

}
公共静态类AppExtensions
{
公共静态IApplicationBuilder获取(此IApplicationBuilder应用程序,
字符串路径,Func运行)
=>app.Use(异步(上下文,下一步)=>
{
if(context.Request.Path==Path)
wait context.Response.WriteAsync(run());
否则等待下一步();
});
}

这只会很方便,因为您使用的是ExpressJS。在一个有成百上千条路由的实际应用程序中,这根本不是你想要的。这是针对一个没有数千行代码的情况,即在一个测试模拟web服务器中,整个服务器可能有10行代码-没有必要为一个很小的应用程序编写控制器和加载大量的MVC库什么?什么沉重的图书馆?无论您喜欢与否,都需要加载ASP.NET核心库。MVC,你可以加载它,如果你想,你不必。我在网上发布了一个问题,询问这个功能,发现他们正在为.NET Core 3.0版本Hanks@NevilleNazerane开发它-这正是我所想的!讨论链接: