MVC3 - How do I route to a subsite?
I have a subsite within my main site for site Administration. The site is physically stored in the form
~/Views/Administration/ViewName/Index
With controllers inside
~/Controllers/Admin/ControllerName
I am getting an exception trying to visit the page.
The view 'index' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/ViewName/index.aspx ~/Views/ViewName/index.ascx ~/Views/Shared/index.aspx ~/Views/Shared/index.ascx ~/Views/ViewName/index.cshtml ~/Views/ViewName/index.vbhtml ~/Views/Shared/index.cshtml ~/Views/Shared/index.vbhtml
I added a route
routes.MapRoute(
"Administration", // Route name
"Administration/{controller}/{action}/{id}", // URL with parameters
new { controller = "Administration", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Going directly to the page manually
http://localhost:999/Administration/BaseItem/index
Does not result in exception, but I get no content. This leads me to believe it is not finding the View. What am I doing wrong?
I think the issue is that I have told the route engine how to route to the Controller, but I have not told the system how to r开发者_StackOverflow中文版oute to the View. Where do I tell the system where the views are?
The problem is not with the routes, but with the design. The view engine cannot find your view, because it cannot find the correct path, since the default view engines are not designed to search for a subsite.
Instead of creating a subsite, make Administration an Area
in your project. In AdministrationAreaRegistration.cs
, you will set a route similar to the route you added. Place your views in the Views folder inside the Administration folder (inside the Area folder), and everything will work properly.
精彩评论