MVC3 区域路由

发布于 2024-12-28 16:58:31 字数 2507 浏览 1 评论 0 原文

我有一个包含两个区域和一个根区域的 MVC3 应用程序。总体结构如下 Root

- 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 }, 

默认路由,那么我可以从“注册”页面开始。

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

我的第二个问题与这些地区本身有关。由于它们都有一个“家庭”控制器,我已将区域名称放在一个控制器前面以进行区分,但我不想这样做。目前,“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”的 URL

,而 ManagerAreaRegistraion.cs 的代码

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

        );

给出“localhost***/Home/”的 URL "

我的第二个问题是:如何为管理者和参与者(或任意数量的区域)提供“localhost**/Home”的 URL,而不必拥有URL 中显示的区域名称?

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

I have a MVC3 application with two areas and a root area. The general structure looks like
Root

- 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

I am having two problems with routing. The first is that I am unable to navigate to any pages in the Root/Views folders except the one set as default in the Global.asax.cs file. The Global.asax.cs file looks like:

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();
...

And the code in Root/Views/Home/Index.cshtml which is the start page looks like:

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

The two area links work fine as I have added routes into the registration files in each area, but the link to Accounts/Register which is another page in the root gives a 'resources not found error'. However, if I change the Global.asax.cs route to have

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

in the default route, then I can start on the Register page.

So my first question is: How do I use routes to be able to access both pages in the Areas and in the Root (ie the Accounts/Register page)?

My second question has to do with the areas themselves. Since they both have a 'Home' controller, I have put the area name in front of one to distinguish it, but I would like to not have to do this. Currently the 'ParticipantAreaRegistration.cs file has the code:

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

which gives URL's of "localhost**/Participant/Home"

while the ManagerAreaRegistraion.cs has code

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

        );

which gives URL's of "localhost***/Home/"

My second question is : How can I have the URL of "localhost**/Home for both Manager and Participant (or for any number of areas) without having to have the Area name displayed in the URL?

I know this question is similar to others already on file, but I have scoured these to no avail and am currently drowning in inefficiency, so I thought I would try asking with specificity. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓦然回首 2025-01-04 16:58:31

您可以使用自定义路由。

与此类似的东西:

MVC 2 区域注册路由顺序

使用上述问题的解决方案,您可以编写自定义的路由顺序。

在我的一个应用程序中,我有名为“管理”、“博客”、“成员”的区域和
民众。我已将公共区域路由为 url:
http://localhost:4000/,管理员为:http://localhost:4000/admin,博客
如: http://localhost:4000/blog 等。如果你想要我的代码,我可以给
你。

You can use custom routing.

Something similar to this one:

MVC 2 AreaRegistration Routes Order

Using the solution in the above problem, you can write custom order of routing.

In one of my application, I have areas named Admin,Blog,Members and
Public. I have routed the Public area as the url:
http://localhost:4000/, Admin as: http://localhost:4000/admin, blog
as: http://localhost:4000/blog, etc.. If you want my code, I can give
you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文