我有一个包含两个区域和一个根区域的 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.
发布评论
评论(1)