MVC使用MVC中的区域进入错误的视图

时间:2018-11-20 01:08:10

标签: asp.net asp.net-mvc asp.net-mvc-routing asp.net-mvc-areas

我有一个区域名称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 /索引。不知道我在做什么错。

这是家庭控制器(当我进入家庭/索引时我想打的控制器)

    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区域中的家庭控制器(即使我不应该去家/索引时我也要点击的那个控制器)

 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动作索引。因此以下所有内容都应带我们去了解这一观点。

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。

在我尝试转到无法找到控制器的“ 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 });
        }
    }
}

1 个答案:

答案 0 :(得分:2)

从评论中我读到,您希望http://localhost:50908/HRHome/IndexHRHomeController区域中的HR处理。

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

通过显式且唯一的路由模板拥有此路由,可以消除与其他路由的任何冲突,包括来自默认区域的冲突。

AreaRegistration而不是RouteConfig内部注册此路由很重要,否则视图不会从views内的相应HR文件夹中解析出来区域。

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" }
        );
}