Bryan Dellinger
Bryan Dellinger

Reputation: 5294

MVC going to the wrong view using area in mvc

I have an area name HR. here is the HRAreaRegistration.CS

namespace WebApplication1.Areas.HR
{
    public class HRAreaRegistration : AreaRegistration 
    {
        public override string AreaName 
        {
            get 
            {
                return "HR";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context) 
        {
            context.MapRoute(
              "HR_default2",
              "HR/{controller}/{action}/{id}",
              new { action = "Index", id = UrlParameter.Optional }
          );
            context.MapRoute(
               "HR_default1",
               "{controller}/{action}/{id}",
               new { action = "Index", id = UrlParameter.Optional }
           );

        }
    }
}

in RouteConfig.cs

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

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                new[] { "WebApplication1.Controllers" }
            );
        }
    }

I was hoping that since I prioritized the main controller namespace when I go to Home/Index I would hit the view and controller HomeController/Index. Instead I am going to the Home Controller in the HR Area. HR/HomeController/Index. not sure what I am doing wrong.

here is home controller (the one I would like to hit when I go to Home/Index)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;

    namespace WebApplication1.Controllers
    {
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                return View();
            }

and here is the home controller in the hr area (the one I am hitting when I go to Home/Index even though I shouldn't be)

 using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Web.Mvc;

        namespace WebApplication1.Areas.HR.Controllers
        {
            public class HomeController : Controller
            {
                // GET: HR/Home
                public ActionResult Index()
                {
                    return View();
                }
            }
        }

/***********Editing my question in response to pfx answer********************/

for this application if a certain variable is set then the default page should be in the HR area. specifically HR area Controller OST action Index. so all of the following should take us to that view.

http://nrdephrs01/hrpa
http://nrdephrs01/hrpa/ost
http://nrdephrs01/hrpa/ost/index
http://nrdephrs01/hrpa/hr/ost/
http://nrdephrs01/hrpa/hr/ost/index

now when they get to this default page there is a link that is to take them to Users/Details/1524 so this controller is not in an area. it is just Controller = Users Action = Details.

here are my routes which work until I try to go to the Users/Details/1524 where it can not find the controller.

HRAreaRegistration

namespace Portal.UI.Areas.HR
{
    using System.Web.Mvc;

    public class HRAreaRegistration : AreaRegistration 
    {
        public override string AreaName 
        {
            get 
            {
                return "HR";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context) 
        {

            context.MapRoute(
                "HR_default",
                "HR/{controller}/{action}/{id}",
                new { controller = "Home", action = "Index", id = UrlParameter.Optional });

            // if the application is Out of State travel there will be an appsetting called OST set to true.
            // for OST applications have the main default page be HR/OST/Index
            string ost = System.Configuration.ConfigurationManager.AppSettings["OST"];

            if (ost == "true")
            {
                context.MapRoute(
                "Details",
                 "Users/{action}/{id}",
                 new { controller = "Users", action = "Index", id = UrlParameter.Optional });

                context.MapRoute(
                 "HR_default1",
                  "{controller}/{action}/{id}",
                  new { controller = "OST", action = "Index", id = UrlParameter.Optional });
            }
        }
    }
}

and here is RouteConfig.cs

{
    using System.Web.Mvc;
    using System.Web.Routing;

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

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
        }
    }
}

Upvotes: 1

Views: 847

Answers (1)

pfx
pfx

Reputation: 23214

From the comments I read that you want http://localhost:50908/HRHome/Index to be handled by the HRHomeControllerin the HR area.

To do so you have to define an explicit route in HRAreaRegistration.cs for HRHome, replacing {controller} with HRHome in the route template.

Having this route with an explicit and unique route template eliminates any conflicts with other routes, including those from the default area.

It is important to register this route from within the AreaRegistration instead of RouteConfig, otherwise the views don't get resolved from the appropriate views folder within the HR area.

context.MapRoute(
    name: "HRHome"
    url: "HRHome/{action}/{id}",
    defaults: new { 
        controller = "HRHome", 
        action = "Index", 
        id = UrlParameter.Optional 
        },
    namespaces: new [] { "WebApplication1.Areas.HR.Controllers" }
    );

The full area registration looks like:

public override void RegisterArea(AreaRegistrationContext context) 
{
    context.MapRoute(
        name: "HRHome"
        url: "HRHome/{action}/{id}",
        defaults: new { 
            controller = "HRHome", 
            action = "Index", 
            id = UrlParameter.Optional 
            },
        namespaces: new [] { "WebApplication1.Areas.HR.Controllers" }
        );


    context.MapRoute(
        name: "HR_default"
        url: "HR/{controller}/{action}/{id}",,
        defaults: new { 
            controller = "HRHome", 
            action = "Index", 
            id = UrlParameter.Optional 
            },
        namespaces: new [] { "WebApplication1.Areas.HR.Controllers" }
        );
}

Upvotes: 4

Related Questions