I have a project that I am upgrading from MVC 2 -> MVC 4. During the transition from MVC 2 -> MVC 3, I noticed that some of my hyperlinks broke and were no longer matching the routes as defined before. I have the following project structure:
我有一个项目,我正在从MVC 2升级 - > MVC 4.在从MVC 2 - > MVC 3过渡期间,我注意到我的一些超链接断了,不再匹配之前定义的路由。我有以下项目结构:
...
App_Data
Areas
Test
Controllers
TestController
Views
Models
Controllers
PartialController
Views
Scripts
...
The links that are generated are in the format /Test/Partial/Render
. If I go back and run the project before the migration, the PartialController
Render
action is hit as expected. However, now the app says that it can't find an action because there is no Test/Partial
controller. I actually am not sure how it worked before, but can't seem to get it to map correctly.
生成的链接采用格式/ Test / Partial / Render格式。如果我在迁移之前返回并运行项目,则会按预期命中PartialController Render操作。但是,现在应用程序说它找不到动作,因为没有Test / Partial控制器。我实际上不确定它之前是如何工作的,但似乎无法让它正确映射。
Is there something I'm missing? Here is the relevant code:
有什么我想念的吗?这是相关的代码:
Global.asax.cs:
...
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
...
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = "" }
);
TestAreaRegistration.cs:
context.MapRoute(
"Test_default",
"Test/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
I did move the Controllers folder up a level to be more in-line with how the project should be structured; beforehand it was in the Areas folder itself. However, none of this worked before I moved that, so I doubt that is the case. I also thought that Autofac had something to do with this, but I am less certain of that because if I shave the "Test" portion out of the URL it matches as expected.
我确实将Controllers文件夹向上移动了一个级别,以更加符合项目的结构;事先它在Areas文件夹本身。但是,在我移动之前,这一切都没有奏效,所以我怀疑是这样的。我还认为Autofac与此有关,但我不太确定,因为如果我将URL中的“Test”部分与预期匹配。
I guess this all boils down to a question on how to check in a "generic" controllers directory first, before matching to an area-specific controller directory, even with an area specified in the URL.
我想这一切都归结为如何在匹配特定于区域的控制器目录之前首先签入“通用”控制器目录的问题,即使是在URL中指定的区域。
Thanks in advance for your help!
在此先感谢您的帮助!
EDIT: If it helps, I noticed that in the existing MVC 2 solution, if I go to Test/Home
for example, it will invoke the Index
method of the HomeController
. However, this does not happen in the new solution. Is this because there is no View associated with the new routes? I have not made new .cshtml files for each of the corresponding .ascx files yet, but I didn't think that should matter much as they are used otherwise.
编辑:如果有帮助,我注意到在现有的MVC 2解决方案中,如果我去Test / Home,它将调用HomeController的Index方法。但是,在新解决方案中不会发生这种情况。这是因为没有与新路线相关的View吗?我还没有为每个相应的.ascx文件创建新的.cshtml文件,但我认为这并不重要,因为否则会使用它们。
1 个解决方案
#1
0
So apparently the issue was related to namespaces. I had seen that answer on questions like this, but those had to do with collisions finding views. Here is the line I ended up adding to each of my Area registrations:
显然这个问题与命名空间有关。我在这样的问题上看到了答案,但那些与碰撞发现观点有关。以下是我最后添加到我的每个区域注册的行:
context.MapRoute(
"Test_default",
"Test/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new string[] { "Project.Web.Controllers", "Project.Web.Areas.Test.Controllers" } //added this line
);
#1
0
So apparently the issue was related to namespaces. I had seen that answer on questions like this, but those had to do with collisions finding views. Here is the line I ended up adding to each of my Area registrations:
显然这个问题与命名空间有关。我在这样的问题上看到了答案,但那些与碰撞发现观点有关。以下是我最后添加到我的每个区域注册的行:
context.MapRoute(
"Test_default",
"Test/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new string[] { "Project.Web.Controllers", "Project.Web.Areas.Test.Controllers" } //added this line
);