我有一个地区名称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中/索引 . 不确定我做错了什么 .
这里是家庭控制器(当我回到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
现在当他们到达这个默认页面时,有一个链接将他们带到Users / Details / 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 });
}
}
}
1 回答
从我读到的评论中,您希望
http://localhost:50908/HRHome/Index
由HR
区域中的HRHomeController
处理 .为此,您必须在
HRAreaRegistration.cs
中为HRHome
定义显式路径,在路径模板中将{controller}
替换为HRHome
.使用具有显式且唯一的路由模板的此路由消除了与其他路由的任何冲突,包括来自默认区域的路由 .
从
AreaRegistration
而不是RouteConfig
注册此路由非常重要,否则视图无法从HR
区域内的相应views
文件夹中解析 .全区域注册如下: