ASP.NET MVC 3区域 - 无法使用自定义路由查找视图

RPM*_*984 4 asp.net-mvc asp.net-mvc-routing asp.net-mvc-areas asp.net-mvc-3

我在一个区域有一条自定义路线如下:

context.Routes.Add(
                "SearchIndex - By Location - USA",
                new CountryTypeSpecificRoute(
                    CountryType.UnitedStates,
                    "search/{locationType}-in-{query}",
                    new { controller = "Search", action = "Index", query = UrlParameter.Optional },
                    new { locationType = new UsaLocationSearchRouteConstraint() })
            );
Run Code Online (Sandbox Code Playgroud)

示例网址:

/搜索/社区功能于新约克城

解决行动很好.但它无法找到View.

未找到视图"索引"或其主数据或视图引擎不支持搜索的位置.搜索了以下位置:〜/ Views/Search/Index.cshtml~/Views/Shared/Index.cshtml

该视图位于〜/ Areas/Search/Views/Search/Index.cshtml中

为什么不看那里?

如果我这样做context.MapRoute而不是context.Routes.Add,那就有效.所以它似乎与我使用自定义路线的事实有关?

有任何想法吗?

RPM*_*984 5

通过这个答案解决了它

我制作了自定义路由器IRouteWithArea(在ctor中接收),并相应地更新我的注册:

context.Routes.Add(
                "SearchIndex - By Location - USA",
                new CountryTypeSpecificRoute(
                    CountryType.UnitedStates,
                    "search/{locationType}-in-{query}",
                    new { controller = "Search", action = "Index", query = UrlParameter.Optional },
                    new { locationType = new UsaLocationSearchRouteConstraint() },
                    "Search")
            );
Run Code Online (Sandbox Code Playgroud)

请注意最后一个参数"搜索" - 表示区域名称.

不知道它是如何工作的,但确实如此.猜猜内部路由引擎寻找实现的路由IRouteWithArea.

问题解决了!