RPM1984
RPM1984

Reputation: 73112

ASP.NET MVC 3 Areas - Can't Find View With Custom Route

I've got a custom route in an area as follows:

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() })
            );

Example URL:

/search/neighborhoods-in-new-york-city

Resolves the action fine. But it can't find the View.

The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/Search/Index.cshtml ~/Views/Shared/Index.cshtml

The view lives in ~/Areas/Search/Views/Search/Index.cshtml

Why didn't it look there?

If i docontext.MapRoute instead of context.Routes.Add, it works. So it seems like it's got to do with the fact i'm using a custom route?

Any ideas?

Upvotes: 4

Views: 1923

Answers (1)

RPM1984
RPM1984

Reputation: 73112

Solved it thanks to this answer

I made my custom route implement IRouteWithArea (take it in in the ctor), and update my registration accordingly:

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")
            );

Note the last parameter "Search" - for the area name.

Don't know how that works, but it does. Guess the internal routing engine looks for routes that implement IRouteWithArea.

Problem solved!

Upvotes: 5

Related Questions