【问题标题】:Add new route constraint in .net5/vNext/MVC6在 .net5/vNext/MVC6 中添加新的路由约束
【发布时间】:2015-10-13 11:55:28
【问题描述】:

在 .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 中在哪里执行此操作。有人可以帮忙吗?我在所有控制器上使用属性路由

【问题讨论】:

标签: c# asp.net .net asp.net-core asp.net-core-mvc


【解决方案1】:

您可以在 Startup 类的 ConfigureServices 部分进行注册。

    public virtual IServiceProvider ConfigureServices(IServiceCollection services)
    {

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-20
    相关资源
    最近更新 更多