MVC3 RouteHandler 结果忽略已解析路由中的区域 - 为什么?
我正在努力将 MVC3 应用程序拆分为两个区域。现有的应用程序将进入一个区域(V1),我们正在第二个区域(V2)开始重新设计。
我将视图、模型、控制器等全部移至 MyApp\Areas\V1\xxx 文件夹中,并且我已经能够验证大部分内容是否正在加载。我将 V1 的所有路由注册移至 V1AreaRegistration.cs 文件中,并将其修改为使用 context.MapRoute
而不是 RouteTable.Routes.MapRoute
。
使用 Phil Haack 的 RouteDebugger 查看路由,一切看起来都很好 - 我的 V2 路由正在按照应有的方式解析,并且基本的 V1 路由也可以工作。不起作用的是 V1 路由,其中我定义了一个自定义 MvcRouteHandler
,如下所示:
context.MapRoute(
"MyRouteName",
"{myRouteVal}/{controller}/{action}",
new { area = "V1", controller = "DefaultController", action = "Index" },
new { controller = _someRouteConstraint},
new string[] { "My.Custom.Project.WebLibrary.Areas.V1.Controllers" },
_myCustomRouteHandler);
如果我从此调用中删除 _myCustomRouteHandler
,它会很好地工作并将我发送到V1 区域下的正确视图。就位后,路由似乎忽略了它被注册为区域路由的事实,并且我收到一个黄色错误页面,上面写着:
The view 'Index' or its master was not found or no view engine
supports the searched locations. The following locations were searched:
~/Views/DefaultController/Index.cshtml
~/Views/DefaultController/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml
~/Views/DefaultController/Index.aspx
~/Views/DefaultController/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
我需要能够使用路由处理程序 - 它对 < code>{myRoutVal} 参数,如果无效则重定向到错误页面。
帮助!!
I am working on splitting an MVC3 Application into two Areas. The existing application is going into one area (V1) and we are beginning a redesign in the second area (V2).
I have the Views, Models, Controllers, etc all moved into the MyApp\Areas\V1\xxx folders, and I have been able to verify that things are loading as they should for the most part. I moved all the Route registration for V1 into the V1AreaRegistration.cs file, and modified it to use context.MapRoute
instead of RouteTable.Routes.MapRoute
.
Looking at the routing with Phil Haack's RouteDebugger, everything looks good - my V2 routes are resolving as they should, and the basic V1 routes work. What is NOT working are the V1 routes where I have a custom MvcRouteHandler
defined such as this:
context.MapRoute(
"MyRouteName",
"{myRouteVal}/{controller}/{action}",
new { area = "V1", controller = "DefaultController", action = "Index" },
new { controller = _someRouteConstraint},
new string[] { "My.Custom.Project.WebLibrary.Areas.V1.Controllers" },
_myCustomRouteHandler);
If I remove the _myCustomRouteHandler
from this call, it works great and sends me to the correct view under the V1 Area. With it in place, the routing seems to ignore the fact that it is registered as an Area route, and I get a yellow error page that says:
The view 'Index' or its master was not found or no view engine
supports the searched locations. The following locations were searched:
~/Views/DefaultController/Index.cshtml
~/Views/DefaultController/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml
~/Views/DefaultController/Index.aspx
~/Views/DefaultController/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
I need to be able to use the route handler - it does some validation work on the {myRoutVal}
parameter and redirects to an error page if it is invalid.
Help!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发现问题了。我们有一个用于
AreaRegistrationContext
的扩展方法,您可以在上面看到引用的 (context.MapRoute
) - 在该方法中,我没有设置route.DataTokens["Area "]
值正确,因此当它尝试查找视图时,它不会在该区域中查找。将以下行添加到我的
AreaRegistrationContext
扩展方法中解决了问题:Found the problem. We have an extension method for
AreaRegistrationContext
that you see referenced above (context.MapRoute
) - in that method, I am not setting theroute.DataTokens["Area"]
value correctly, so when it tries to find the view, it doesn't look in the area.Adding the following line to my
AreaRegistrationContext
extension method solved the problem: