如何在asp.net核心中编写中间件作为自定义路由器?

时间:2017-01-07 08:14:54

标签: c# asp.net-core asp.net-core-middleware asp.net-core-routing

我想转换这段代码:

var trackPackageRouteHandler = new RouteHandler(context =>
    {
        var routeValues = context.GetRouteData().Values;
        return context.Response.WriteAsync(
            $"Hello! Route values: {string.Join(", ", routeValues)}");
    });

var routeBuilder = new RouteBuilder(app, trackPackageRouteHandler);

routeBuilder.MapRoute(
    "Track Package Route",
    "package/{operation:regex(^track|create|detonate$)}/{id:int}");

routeBuilder.MapGet("hello/{name}", context =>
{
    var name = context.GetRouteValue("name");
    // This is the route handler when HTTP GET "hello/<anything>"  matches
    // To match HTTP GET "hello/<anything>/<anything>,
    // use routeBuilder.MapGet("hello/{*name}"
    return context.Response.WriteAsync($"Hi, {name}!");
});

var routes = routeBuilder.Build();
app.UseRouter(routes);

进入这个:

app.UseHelloRouter();

但我不想使用路由

以外的任何服务

1 个答案:

答案 0 :(得分:2)

您可以创建一个扩展方法来封装路由器配置

public static class HelloRouterExtensions {

    public static IApplicationBuilder UseHelloRouter(this IApplicationBuilder app) {
        var trackPackageRouteHandler = new RouteHandler(context => {
            var routeValues = context.GetRouteData().Values;
            return context.Response.WriteAsync(
                $"Hello! Route values: {string.Join(", ", routeValues)}");
        });

        var routeBuilder = new RouteBuilder(app, trackPackageRouteHandler);

        routeBuilder.MapRoute(
            "Track Package Route",
            "package/{operation:regex(^track|create|detonate$)}/{id:int}");

        routeBuilder.MapGet("hello/{name}", context => {
            var name = context.GetRouteValue("name");
            // This is the route handler when HTTP GET "hello/<anything>"  matches
            // To match HTTP GET "hello/<anything>/<anything>,
            // use routeBuilder.MapGet("hello/{*name}"
            return context.Response.WriteAsync($"Hi, {name}!");
        });

        var routes = routeBuilder.Build();
        app.UseRouter(routes);
        return app;
    }
}

这基本上改变了这种配置

public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
    var trackPackageRouteHandler = new RouteHandler(context =>
    {
        var routeValues = context.GetRouteData().Values;
        return context.Response.WriteAsync(
            $"Hello! Route values: {string.Join(", ", routeValues)}");
    });

    var routeBuilder = new RouteBuilder(app, trackPackageRouteHandler);

    routeBuilder.MapRoute(
        "Track Package Route",
        "package/{operation:regex(^track|create|detonate$)}/{id:int}");

    routeBuilder.MapGet("hello/{name}", context =>
    {
        var name = context.GetRouteValue("name");
        // This is the route handler when HTTP GET "hello/<anything>"  matches
        // To match HTTP GET "hello/<anything>/<anything>,
        // use routeBuilder.MapGet("hello/{*name}"
        return context.Response.WriteAsync($"Hi, {name}!");
    });

    var routes = routeBuilder.Build();
    app.UseRouter(routes);
}

进入此配置。

public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) {
    app.UseHelloRouter();
}