【发布时间】:2019-04-22 09:56:30
【问题描述】:
我有一个地区名称 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。相反,我要去人力资源区的家庭控制器。人力资源/家庭控制器/索引。不知道我做错了什么。
这里是 home 控制器(我去 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 领域控制器 OST 行动指数。因此,以下所有内容都应将我们带到该视图。
http://nrdephrs01/hrpa
http://nrdephrs01/hrpa/ost
http://nrdephrs01/hrpa/ost/index
http://nrdephrs01/hrpa/hr/ost/
http://nrdephrs01/hrpa/hr/ost/index
现在,当他们到达此默认页面时,会有一个链接将他们带到用户/详细信息/1524,因此此控制器不在一个区域中。它只是 Controller = Users Action = 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 });
}
}
}
【问题讨论】:
-
因为您的
HR_default1路由注册缺少区域前缀。只需删除MapRoute电话 -
我不想在它前面加上 HR,这样如果有一个仅在 HR 区域中的控制器操作,比如 HRHomeController,我可以同时使用 HR/HRHome/Index 和 HRHome/Index。这就是为什么我有两条路线
-
HR_default1配置与默认路由具有相同的模式,这是多余的,应该删除。为什么不尝试设置HR/{action}/{id}路由? -
当我删除 HR_default 1 时,我无法使用localhost:50908/HRHome/Index 击中我的 HRHomeController 我只能使用 localhost:50908/HR/HRHome/Index 击中它,当我将这条路线放入时,我可以双向击中它。所以我想我需要额外的路线
-
将
new[] { "WebApplication1.Areas.HR.Controllers" }放在HR_default1定义中怎么样?请注意,区域路由以最高顺序放置,因此如果您有相同的 URL 路由路径,它首先匹配区域路由而不是默认路由。
标签: asp.net asp.net-mvc asp.net-mvc-routing asp.net-mvc-areas