URL 中的下划线不适用于属性路由

Met*_*hor 3 asp.net-mvc asp.net-mvc-5.1

Visual Studio 2013 IDE 中出现以下问题。部署到 IIS 时,它似乎可以正常工作。

我有一个如下所示的属性路由:

    [Route("SiteCode/{sites:alpha?}")] 
Run Code Online (Sandbox Code Playgroud)

当我请求 /SiteCode 时,它​​工作正常。当我包含一个参数(例如 /SiteCode/something)时,它也能正常工作。但是,当 URL 为 /SiteCode/some_thing 时,它找不到控制器并且我收到 404。

这个在 SiteCodes 参数中使用下划线:

[Route("Performance/{CountryIDs?}/{StateIDs?}/{SiteCodes?}/{Titles?}/{StartDate:datetime?}/{EndDate:datetime?}")]
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

Nov*_*kov 6

根据http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2#constraints alpha约束只匹配大写和小写拉丁字符和下划线不是其中之一。

如果你想允许下划线,你可以使用以下约束:

regex(^[a-zA-Z_]+$)
Run Code Online (Sandbox Code Playgroud)