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 });
}
}
}
asp.net asp.net-mvc asp.net-mvc-routing asp.net-mvc-areas
|
show 3 more comments
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 });
}
}
}
asp.net asp.net-mvc asp.net-mvc-routing asp.net-mvc-areas
Because yourHR_default1route registration is missing the area prefix. Just remove thatMapRoutecall
– Shyju
Nov 20 '18 at 1:10
I don't want to prefix it with HR so that if there a controller action that is only in the HR area like HRHomeController I can hit it with both HR/HRHome/Index and HRHome/Index. that is why I have the two routes
– Bryan Dellinger
Nov 20 '18 at 1:15
HR_default1configuration has same pattern as default route, this is redundant and should be removed. Why don't you try settingHR/{action}/{id}route?
– Tetsuya Yamamoto
Nov 20 '18 at 1:25
when I remove HR_default 1 I cannot hit my HRHomeController with localhost:50908/HRHome/Index I can only hit it with localhost:50908/HR/HRHome/Index when I put that route in I can hit it both ways. so I think I need the extra route
– Bryan Dellinger
Nov 20 '18 at 1:32
1
How about puttingnew { "WebApplication1.Areas.HR.Controllers" }insideHR_default1definition? Note that area route is placed in topmost order, therefore if you have same URL route path it matches the area route first instead of the default route.
– Tetsuya Yamamoto
Nov 21 '18 at 2:36
|
show 3 more comments
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 });
}
}
}
asp.net asp.net-mvc asp.net-mvc-routing asp.net-mvc-areas
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 });
}
}
}
asp.net asp.net-mvc asp.net-mvc-routing asp.net-mvc-areas
asp.net asp.net-mvc asp.net-mvc-routing asp.net-mvc-areas
edited Nov 28 '18 at 18:05
Bryan Dellinger
asked Nov 20 '18 at 1:08
Bryan DellingerBryan Dellinger
2,51231638
2,51231638
Because yourHR_default1route registration is missing the area prefix. Just remove thatMapRoutecall
– Shyju
Nov 20 '18 at 1:10
I don't want to prefix it with HR so that if there a controller action that is only in the HR area like HRHomeController I can hit it with both HR/HRHome/Index and HRHome/Index. that is why I have the two routes
– Bryan Dellinger
Nov 20 '18 at 1:15
HR_default1configuration has same pattern as default route, this is redundant and should be removed. Why don't you try settingHR/{action}/{id}route?
– Tetsuya Yamamoto
Nov 20 '18 at 1:25
when I remove HR_default 1 I cannot hit my HRHomeController with localhost:50908/HRHome/Index I can only hit it with localhost:50908/HR/HRHome/Index when I put that route in I can hit it both ways. so I think I need the extra route
– Bryan Dellinger
Nov 20 '18 at 1:32
1
How about puttingnew { "WebApplication1.Areas.HR.Controllers" }insideHR_default1definition? Note that area route is placed in topmost order, therefore if you have same URL route path it matches the area route first instead of the default route.
– Tetsuya Yamamoto
Nov 21 '18 at 2:36
|
show 3 more comments
Because yourHR_default1route registration is missing the area prefix. Just remove thatMapRoutecall
– Shyju
Nov 20 '18 at 1:10
I don't want to prefix it with HR so that if there a controller action that is only in the HR area like HRHomeController I can hit it with both HR/HRHome/Index and HRHome/Index. that is why I have the two routes
– Bryan Dellinger
Nov 20 '18 at 1:15
HR_default1configuration has same pattern as default route, this is redundant and should be removed. Why don't you try settingHR/{action}/{id}route?
– Tetsuya Yamamoto
Nov 20 '18 at 1:25
when I remove HR_default 1 I cannot hit my HRHomeController with localhost:50908/HRHome/Index I can only hit it with localhost:50908/HR/HRHome/Index when I put that route in I can hit it both ways. so I think I need the extra route
– Bryan Dellinger
Nov 20 '18 at 1:32
1
How about puttingnew { "WebApplication1.Areas.HR.Controllers" }insideHR_default1definition? Note that area route is placed in topmost order, therefore if you have same URL route path it matches the area route first instead of the default route.
– Tetsuya Yamamoto
Nov 21 '18 at 2:36
Because your
HR_default1 route registration is missing the area prefix. Just remove that MapRoute call– Shyju
Nov 20 '18 at 1:10
Because your
HR_default1 route registration is missing the area prefix. Just remove that MapRoute call– Shyju
Nov 20 '18 at 1:10
I don't want to prefix it with HR so that if there a controller action that is only in the HR area like HRHomeController I can hit it with both HR/HRHome/Index and HRHome/Index. that is why I have the two routes
– Bryan Dellinger
Nov 20 '18 at 1:15
I don't want to prefix it with HR so that if there a controller action that is only in the HR area like HRHomeController I can hit it with both HR/HRHome/Index and HRHome/Index. that is why I have the two routes
– Bryan Dellinger
Nov 20 '18 at 1:15
HR_default1 configuration has same pattern as default route, this is redundant and should be removed. Why don't you try setting HR/{action}/{id} route?– Tetsuya Yamamoto
Nov 20 '18 at 1:25
HR_default1 configuration has same pattern as default route, this is redundant and should be removed. Why don't you try setting HR/{action}/{id} route?– Tetsuya Yamamoto
Nov 20 '18 at 1:25
when I remove HR_default 1 I cannot hit my HRHomeController with localhost:50908/HRHome/Index I can only hit it with localhost:50908/HR/HRHome/Index when I put that route in I can hit it both ways. so I think I need the extra route
– Bryan Dellinger
Nov 20 '18 at 1:32
when I remove HR_default 1 I cannot hit my HRHomeController with localhost:50908/HRHome/Index I can only hit it with localhost:50908/HR/HRHome/Index when I put that route in I can hit it both ways. so I think I need the extra route
– Bryan Dellinger
Nov 20 '18 at 1:32
1
1
How about putting
new { "WebApplication1.Areas.HR.Controllers" } inside HR_default1 definition? Note that area route is placed in topmost order, therefore if you have same URL route path it matches the area route first instead of the default route.– Tetsuya Yamamoto
Nov 21 '18 at 2:36
How about putting
new { "WebApplication1.Areas.HR.Controllers" } inside HR_default1 definition? Note that area route is placed in topmost order, therefore if you have same URL route path it matches the area route first instead of the default route.– Tetsuya Yamamoto
Nov 21 '18 at 2:36
|
show 3 more comments
1 Answer
1
active
oldest
votes
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" }
);
}
thank you I updated my question after attempting your solution. I am unable to hit the controller that is not in the HR area.
– Bryan Dellinger
Nov 28 '18 at 18:05
See my comment on the question about your routes setup.
– pfx
Nov 28 '18 at 18:29
thanks pfx that got me on the correct track. seems to be working now. I can't award the bounty for 17 hours but then I will do so.
– Bryan Dellinger
Nov 28 '18 at 19:30
Your welcome! This was a really good brainteaser; an exceptional routing setup, but valid.
– pfx
Nov 28 '18 at 19:32
Good job holding your ground on the changes to the OP. Well earned bounty.
– Nkosi
Nov 28 '18 at 22:11
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384820%2fmvc-going-to-the-wrong-view-using-area-in-mvc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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" }
);
}
thank you I updated my question after attempting your solution. I am unable to hit the controller that is not in the HR area.
– Bryan Dellinger
Nov 28 '18 at 18:05
See my comment on the question about your routes setup.
– pfx
Nov 28 '18 at 18:29
thanks pfx that got me on the correct track. seems to be working now. I can't award the bounty for 17 hours but then I will do so.
– Bryan Dellinger
Nov 28 '18 at 19:30
Your welcome! This was a really good brainteaser; an exceptional routing setup, but valid.
– pfx
Nov 28 '18 at 19:32
Good job holding your ground on the changes to the OP. Well earned bounty.
– Nkosi
Nov 28 '18 at 22:11
|
show 1 more comment
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" }
);
}
thank you I updated my question after attempting your solution. I am unable to hit the controller that is not in the HR area.
– Bryan Dellinger
Nov 28 '18 at 18:05
See my comment on the question about your routes setup.
– pfx
Nov 28 '18 at 18:29
thanks pfx that got me on the correct track. seems to be working now. I can't award the bounty for 17 hours but then I will do so.
– Bryan Dellinger
Nov 28 '18 at 19:30
Your welcome! This was a really good brainteaser; an exceptional routing setup, but valid.
– pfx
Nov 28 '18 at 19:32
Good job holding your ground on the changes to the OP. Well earned bounty.
– Nkosi
Nov 28 '18 at 22:11
|
show 1 more comment
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" }
);
}
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" }
);
}
edited Nov 28 '18 at 15:13
answered Nov 28 '18 at 14:36
pfxpfx
5,270121934
5,270121934
thank you I updated my question after attempting your solution. I am unable to hit the controller that is not in the HR area.
– Bryan Dellinger
Nov 28 '18 at 18:05
See my comment on the question about your routes setup.
– pfx
Nov 28 '18 at 18:29
thanks pfx that got me on the correct track. seems to be working now. I can't award the bounty for 17 hours but then I will do so.
– Bryan Dellinger
Nov 28 '18 at 19:30
Your welcome! This was a really good brainteaser; an exceptional routing setup, but valid.
– pfx
Nov 28 '18 at 19:32
Good job holding your ground on the changes to the OP. Well earned bounty.
– Nkosi
Nov 28 '18 at 22:11
|
show 1 more comment
thank you I updated my question after attempting your solution. I am unable to hit the controller that is not in the HR area.
– Bryan Dellinger
Nov 28 '18 at 18:05
See my comment on the question about your routes setup.
– pfx
Nov 28 '18 at 18:29
thanks pfx that got me on the correct track. seems to be working now. I can't award the bounty for 17 hours but then I will do so.
– Bryan Dellinger
Nov 28 '18 at 19:30
Your welcome! This was a really good brainteaser; an exceptional routing setup, but valid.
– pfx
Nov 28 '18 at 19:32
Good job holding your ground on the changes to the OP. Well earned bounty.
– Nkosi
Nov 28 '18 at 22:11
thank you I updated my question after attempting your solution. I am unable to hit the controller that is not in the HR area.
– Bryan Dellinger
Nov 28 '18 at 18:05
thank you I updated my question after attempting your solution. I am unable to hit the controller that is not in the HR area.
– Bryan Dellinger
Nov 28 '18 at 18:05
See my comment on the question about your routes setup.
– pfx
Nov 28 '18 at 18:29
See my comment on the question about your routes setup.
– pfx
Nov 28 '18 at 18:29
thanks pfx that got me on the correct track. seems to be working now. I can't award the bounty for 17 hours but then I will do so.
– Bryan Dellinger
Nov 28 '18 at 19:30
thanks pfx that got me on the correct track. seems to be working now. I can't award the bounty for 17 hours but then I will do so.
– Bryan Dellinger
Nov 28 '18 at 19:30
Your welcome! This was a really good brainteaser; an exceptional routing setup, but valid.
– pfx
Nov 28 '18 at 19:32
Your welcome! This was a really good brainteaser; an exceptional routing setup, but valid.
– pfx
Nov 28 '18 at 19:32
Good job holding your ground on the changes to the OP. Well earned bounty.
– Nkosi
Nov 28 '18 at 22:11
Good job holding your ground on the changes to the OP. Well earned bounty.
– Nkosi
Nov 28 '18 at 22:11
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384820%2fmvc-going-to-the-wrong-view-using-area-in-mvc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Because your
HR_default1route registration is missing the area prefix. Just remove thatMapRoutecall– Shyju
Nov 20 '18 at 1:10
I don't want to prefix it with HR so that if there a controller action that is only in the HR area like HRHomeController I can hit it with both HR/HRHome/Index and HRHome/Index. that is why I have the two routes
– Bryan Dellinger
Nov 20 '18 at 1:15
HR_default1configuration has same pattern as default route, this is redundant and should be removed. Why don't you try settingHR/{action}/{id}route?– Tetsuya Yamamoto
Nov 20 '18 at 1:25
when I remove HR_default 1 I cannot hit my HRHomeController with localhost:50908/HRHome/Index I can only hit it with localhost:50908/HR/HRHome/Index when I put that route in I can hit it both ways. so I think I need the extra route
– Bryan Dellinger
Nov 20 '18 at 1:32
1
How about putting
new { "WebApplication1.Areas.HR.Controllers" }insideHR_default1definition? Note that area route is placed in topmost order, therefore if you have same URL route path it matches the area route first instead of the default route.– Tetsuya Yamamoto
Nov 21 '18 at 2:36