MVC 在 mvc 中使用区域进入错误的视图
MVC going to the wrong view using area in mvc
我有一个地区名称HR。这是 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 }
);
}
}
}
在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" }
);
}
}
我希望,因为我在转到 Home/Index 时优先考虑主控制器命名空间,所以我会点击视图和控制器 HomeController/Index。相反,我要去人力资源区的家庭控制器。 HR/HomeController/Index。不确定我做错了什么。
这是家庭控制器(我去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();
}
这是 hr 区域的家庭控制器(我去 Home/Index 时正在点击的那个,尽管我不应该这样做)
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();
}
}
}
/***********编辑我的问题以回应 pfx 回答********************/
对于此应用程序,如果设置了某个变量,则默认页面应位于 HR 区域。特别是 HR 区域 Controller OST action Index。因此,以下所有内容都应将我们带入该视图。
http://nrdephrs01/hrpa
http://nrdephrs01/hrpa/ost
http://nrdephrs01/hrpa/ost/index
http://nrdephrs01/hrpa/hr/ost/
http://nrdephrs01/hrpa/hr/ost/index
现在,当他们到达此默认页面时,有一个 link 会将他们带到 Users/Details/1524,因此此控制器不在某个区域中。它只是 Controller = Users Action = Details.
这是我的路线,在我尝试前往找不到控制器的 Users/Details/1524 之前一直有效。
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 });
}
}
}
}
这里是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 });
}
}
}
根据我读到的评论,您希望 http://localhost:50908/HRHome/Index
由 HR
区域中的 HRHomeController
处理。
为此,您必须在 HRAreaRegistration.cs
中为 HRHome
定义显式路由,在路由模板中将 {controller}
替换为 HRHome
。
使该路由具有明确且唯一的路由模板可以消除与其他路由(包括来自默认区域的路由)的任何冲突。
从 AreaRegistration
而不是 RouteConfig
中注册此路由很重要,否则视图不会从 [=14] 中相应的 views
文件夹中解析=] 面积.
context.MapRoute(
name: "HRHome"
url: "HRHome/{action}/{id}",
defaults: new {
controller = "HRHome",
action = "Index",
id = UrlParameter.Optional
},
namespaces: new [] { "WebApplication1.Areas.HR.Controllers" }
);
全区报名如下:
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" }
);
}
我有一个地区名称HR。这是 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 }
);
}
}
}
在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" }
);
}
}
我希望,因为我在转到 Home/Index 时优先考虑主控制器命名空间,所以我会点击视图和控制器 HomeController/Index。相反,我要去人力资源区的家庭控制器。 HR/HomeController/Index。不确定我做错了什么。
这是家庭控制器(我去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();
}
这是 hr 区域的家庭控制器(我去 Home/Index 时正在点击的那个,尽管我不应该这样做)
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();
}
}
}
/***********编辑我的问题以回应 pfx 回答********************/
对于此应用程序,如果设置了某个变量,则默认页面应位于 HR 区域。特别是 HR 区域 Controller OST action Index。因此,以下所有内容都应将我们带入该视图。
http://nrdephrs01/hrpa
http://nrdephrs01/hrpa/ost
http://nrdephrs01/hrpa/ost/index
http://nrdephrs01/hrpa/hr/ost/
http://nrdephrs01/hrpa/hr/ost/index
现在,当他们到达此默认页面时,有一个 link 会将他们带到 Users/Details/1524,因此此控制器不在某个区域中。它只是 Controller = Users Action = Details.
这是我的路线,在我尝试前往找不到控制器的 Users/Details/1524 之前一直有效。
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 });
}
}
}
}
这里是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 });
}
}
}
根据我读到的评论,您希望 http://localhost:50908/HRHome/Index
由 HR
区域中的 HRHomeController
处理。
为此,您必须在 HRAreaRegistration.cs
中为 HRHome
定义显式路由,在路由模板中将 {controller}
替换为 HRHome
。
使该路由具有明确且唯一的路由模板可以消除与其他路由(包括来自默认区域的路由)的任何冲突。
从 AreaRegistration
而不是 RouteConfig
中注册此路由很重要,否则视图不会从 [=14] 中相应的 views
文件夹中解析=] 面积.
context.MapRoute(
name: "HRHome"
url: "HRHome/{action}/{id}",
defaults: new {
controller = "HRHome",
action = "Index",
id = UrlParameter.Optional
},
namespaces: new [] { "WebApplication1.Areas.HR.Controllers" }
);
全区报名如下:
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" }
);
}