Question

When I run the website, I'm expecting the Home view to be displayed but its not finding the view. If i alter the code below, and remove the .mvc from ("{controller}.mvc/{action}" I get the base index view, not the Home view i wish to get from the Home Area.

What I noticed after I removed .mvc from the Route - The Home controller located inside Areas-Home-Controllers-HomeController is being found and executed, when the view gets returned and the AreaViewEngine takes ahold of it, it doesn't see the Areas so it returns base index view... Any clue what could be causing this!?

I have the following RegisterRoutes

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapAreas("{controller}/{action}/{id}",
        "ProjectNamespace.Web",
        new[] { "Home" });

    routes.MapRootArea("{controller}.mvc/{action}",
        "ProjectNamespace.Web",
        new { controller = "Home", action = "Index" });
}

Here is ViewFind located in AreaViewEngine.cs:

   public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
        {
            ViewEngineResult areaResult = null;

            if (controllerContext.RouteData.Values.ContainsKey("area")) 
            {
                string areaViewName = FormatViewName(controllerContext, viewName);
                areaResult = base.FindView(controllerContext, areaViewName, masterName, useCache);
                if (areaResult != null && areaResult.View != null) 
                {
                    return areaResult;
                }
                string sharedAreaViewName = FormatSharedViewName(controllerContext, viewName);
                areaResult = base.FindView(controllerContext, sharedAreaViewName, masterName, useCache);
                if (areaResult != null && areaResult.View != null) 
                {
                    return areaResult;
                }
            }
            return base.FindView(controllerContext, viewName, masterName, useCache);
        }

The BaseController which is located in the default Controllers root directory:

public abstract class BaseController : Controller
{
    public virtual ActionResult Index()
    {
        return View();
    }
}

Here is the Home controller located in Areas-Home-Controllers:

public class HomeController : BaseController
{
    public HomeController()
    {
    }

    public override ActionResult Index()
    {           
        HomeDetailsViewModel model = new HomeDetailsViewModel();
        model.User = "testing username"
        return View("Index", model);
    }
 }

Finally, screenshot of the Solutions Explorer:

enter image description here

Was it helpful?

Solution

for an area you have to specify it. try this

return RedirectToAction("Index", "Home", new { area = "Home" });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top