【问题标题】:MVC going to the wrong view using area in mvcMVC 使用 mvc 中的区域进入错误的视图
【发布时间】: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


【解决方案1】:

从我读到的 cmets 中,您希望 http://localhost:50908/HRHome/Index 由HR 区域中的HRHomeController 处理。

为此,您必须在HRAreaRegistration.cs 中为HRHome 定义一个显式路由,将路由模板中的{controller} 替换为HRHome。

让这条路线带有明确且唯一的路线模板可以消除与其他路线的任何冲突,包括来自默认区域的路线。

从AreaRegistration 而不是RouteConfig 中注册此路由很重要,否则视图无法从HR 区域内的相应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 区域中的控制器。
  • 查看我对有关您的路线设置问题的评论。
  • 感谢 pfx 让我走上了正确的道路。似乎现在正在工作。我不能在 17 小时内奖励赏金,但我会这样做。
  • 欢迎您!这是一个非常好的脑筋急转弯;一个特殊的路由设置,但有效。
  • 在 OP 的变化上坚持你的立场很好。应得的赏金。
猜你喜欢
  • 1970-01-01
  • 2015-02-20
  • 1970-01-01
  • 2011-02-07
  • 2021-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多