Razor View Engine Not Searching Area View Locations
-
06-02-2021 - |
سؤال
I am using the default RazorViewEngine, and Area layout configuration, however when i naviagate to a link which uses a view within the area. I get a error stating the View could not be found and searched in the following locations:
~/Views/Applications/Details.cshtml
~/Views/Applications/Details.vbhtml
~/Views/Shared/Details.cshtml
~/Views/Shared/Details.vbhtml
What I find odd is that it looks as though the view engine does not attempt to search the areas location. What adjustments do I need to make to have the view engine search for views in its area.
Here is the related code I used to define my area.
Global.asax.cs
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteRegistrar.RegisterRoutesTo(RouteTable.Routes);
}
ApplicationAreaRegistration.cs
private void RegisterRoutesTo(RouteCollection routes)
{
routes.MapRoute("Application_default", AreaName + "/{action}/{applicationId}",
new
{
controller = "Applications",
action = "Index",
applicationDomainId = UrlParameter.Optional
}, new { applicationId = @"\d+" });
}
Index.cshtml
@Html.RouteLink(item.Name, "Application_default", new { applicationId = item.Id, action = "Details" })
Physical Directory Layout
Areas \
\Application
\Controllers
-ApplicationsController.cs
\Views
-Details.cshtml
-ApplicationAreaRegistration.cs
المحلول
Are you certain that RegisterRoutesTo()
in ApplicationAreaRegistration.cs
is being invoked? It seems that the Route to your area has not been registered.
I would suggest moving your MapRoute back into the override of RegisterArea
in ApplicationAreaRegistration.cs
.
نصائح أخرى
Your MapRoute
seems to be an issue. Try adding an area
declaration
routes.MapRoute("Application_default",
AreaName + "/{action}/{applicationId}",
new
{
area= "Application",
controller = "Applications",
action = "Index",
applicationDomainId = UrlParameter.Optional
},
new { applicationId = @"\d+" });
Also, just double check the various overloads available for Html.RouteLink
- you may want to add the area the RouteValues
collection
`new {area="Application", applicationId = item.Id, action = "Details"}`
when using RouteLink
outside an area