2015-10-13 23 views
1

在.NET 4.5/2的WebAPI,我可以創建一個約束,使用此代碼在.net5添加新的路由約束/ vNext/MVC6

// 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。有人可以幫忙嗎?我在所有控制器上使用屬性路由

+0

也許你會發現這篇文章有用http://stephenwalther.com/archive/2015/02/07/asp-net-5-deep-dive-routing – Set

+0

另外這款http://stackoverflow.com /問題/ 32583743 /如何對寄存器routeconstraints功能於mvc6 –

回答

3

您可以在Startup類的ConfigureServices部分進行註冊。

public virtual IServiceProvider ConfigureServices(IServiceCollection services) 
    { 

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