带区域的MVC3路由

时间:2012-01-24 00:09:19

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

我有一个MVC3应用程序,它有两个区域和一个根区域。一般结构看起来像 根

- Root/Areas/Manager
    * Views/Home/Index.cshtml
    * ManagerAreaRegistration.cs
- Root/Areas/Participant
    * Views/Home/Index.cshtml
    * ParticipantAreaRegistraion.cs
- Root
    * Views/Home/Index.cshtml
    * Views/Account/Register.cshtml
    * Global.asax.cs

我遇到两个路由问题。第一个是我无法导航到Root / Views文件夹中的任何页面,除了Global.asax.cs文件中的默认设置。 Global.asax.cs文件如下所示:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
   "Default", // Route name
   "{controller}/{action}/{id}", // URL with parameters
   new {controller="Home" , action = "Index", id = UrlParameter.Optional }, 
   new[] { "MVCApplication.Controllers" }    // for areas 
   );
...     
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
...

Root / Views / Home / Index.cshtml中的起始页面中的代码如下所示:

@Html.ActionLink("Accounts","Register", new {area="", controller="Accounts"},null)
@Html.ActionLink("Manager", "Index", new { area = "Manager", controller = "Home" })
@Html.ActionLink("Participant", "Index", new { area = "Participant", controller = "Home" })

两个区域链接工作正常,因为我已将路由添加到每个区域的注册文件中,但是根目录中的另一个页面“帐户/注册”的链接提供了“资源未找到错误”。但是,如果我将Global.asax.cs路由更改为

    new {controller="Accounts" , action = "Register", id = UrlParameter.Optional }, 

在默认路由中,然后我可以在Register页面上启动。

所以我的第一个问题是:如何使用路由来访问区域和根目录中的两个页面(即帐户/注册页面)?

我的第二个问题与这些地区本身有关。由于它们都有一个“Home”控制器,我已经将区域名称放在一个区域前面以区分它,但我不想这样做。目前,'ParticipantAreaRegistration.cs文件的代码为:

  context.MapRoute(
            "Participant_default",
            "Participant/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional },
          new[] { "MvcApplication.Areas.Participant.Controllers"  }    // for areas 
            );

提供“localhost * * / Participant / Home”的网址

而ManagerAreaRegistraion.cs有代码

 context.MapRoute(
            "Manager_default",
            "{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional },
                         new[] { "MvcApplication.Areas.Manager.Controllers" }    // for areas 

        );

,其中包含“localhost *** / Home /”

的网址

我的第二个问题是:如何为经理和参与者(或任何数量的区域)提供“localhost ** / Home”的URL,而无需显示区域名称网址?

我知道这个问题与已经存档的其他问题类似,但是我已经搜索过这些问题无济于事,而且目前正在淹没效率低下,所以我想我会尝试具体询问。感谢。

0 个答案:

没有答案
相关问题