2014-12-23 73 views
0

如何在mvc4中使用正則表達式在路由中添加規則。我想添加一些驗證規則到我的路線。我正在使用asp.net mvc4應用程序。路由中的正則表達式

+1

可能重複[MVC 4:自定義路線](http://stackoverflow.com/questions/12518388/mvc-4-custom-route) –

回答

0

你正在尋找的東西叫做Constraint。您指定約束你的路線定義爲:

routes.MapRoute(
name: "somthing", 
url: "{controller}/{id}", 
defaults: new { controller = "Home", action = "index", id = UrlParameter.Optional }, 
constraints: new { id = @"^[0-9]+$" } 
);