ASP.NET MVC 3 Areas - Cannot Find View with Custom Route

I have a custom route in the 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

Allows the action of a fine. But he cannot find an idea.

The Index view or its wizard is not found, or there is no viewing mechanism that supports the found locations. The following places were search: ~ / Views / Search / Index.cshtml ~ / Views / Shared / Index.cshtml

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

Why didn't he look there?

If I execute context.MapRoute instead of context.Routes.Add , it works. So it looks like this is due to the fact that I'm using my own route?

Any ideas?

+4
source share
1 answer

Solved this thanks to this answer

I followed my own IRouteWithArea route (take it to ctor) and update my post 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") ); 

Pay attention to the last parameter "Search" - for the name of the area.

I don’t know how it works, but it is. Guess that the internal routing engine is looking for routes that implement IRouteWithArea .

The problem is solved!

+5
source

Source: https://habr.com/ru/post/1392590/


All Articles