在 .net5/vNext/MVC6 中添加新的路由约束

Add new route constraint in .net5/vNext/MVC6

在 .NET 4.5/WebApi 2 中,我可以创建一个约束并使用此代码添加它

// add constraint resolvers
var constraintResolver = new DefaultInlineConstraintResolver();
constraintResolver.ConstraintMap.Add("constraintName", typeof(MyCustomConstraint));

// routing
config.MapHttpAttributeRoutes(constraintResolver);

目前在我的 Startup.cs 文件中,我只有这个

public void Configure(IApplicationBuilder app, IServiceProvider serviceProvider)
    {
        // Enable Mvc for controllers
        app.UseMvc();

        // Enable all static file middleware (serving of static files and default files) EXCEPT directory browsing.
        app.UseFileServer();
    }

但我不知道在哪里可以做到这一点 asp.net 5/vNext。有人可以帮忙吗?我在所有控制器上使用属性路由

您可以在 Startup 的 ConfigureServices 部分注册 class。

    public virtual IServiceProvider ConfigureServices(IServiceCollection services)
    {

        services.Configure<RouteOptions>(options =>
                                        options
                                        .ConstraintMap
                                        .Add("constraintName", typeof(MyCustomConstraint)));
    }