MVC3 RouteHandler结果忽略了解决途径区 - 为什么? [英] MVC3 RouteHandler result ignores Area in the resolved Route - why?

查看:220
本文介绍了MVC3 RouteHandler结果忽略了解决途径区 - 为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的分裂MVC3应用程序分成两个区域。现有的应用程序被进入一个区域(V1),我们在第二区(V2)开始重新设计。

我有意见,模型,控制器等全部搬进了MyApp的\\区\\ V1 \\ XXX文件夹,并且我已经能够确认的东西加载,因为他们应该大部分。我感动了所有的路由登记V1到V1AreaRegistration.cs文件,并修改它使用 context.MapRoute 而不是 RouteTable.Routes.MapRoute

看着与菲尔哈克的RouteDebugger路由,一切看起来不错 - 我V2路由解决,他们应该和基本路线V1工作。什么是不工作的是V1的路线,我有一个自定义 MvcRouteHandler 定义像这样的:

  context.MapRoute(
    MyRouteName
    {myRouteVal} / {控制器} / {行动},
    新{面积=V1,控制器=DefaultController,行动=索引},
    新{控制器= _someRouteConstraint},
    新的String [] {My.Custom.Project.WebLibrary.Areas.V1.Controllers},
        _myCustomRouteHandler);

如果我这个调用删除 _myCustomRouteHandler ,它的伟大工程,并送我到V1区下正确的观点。有了它的地方,路由似乎忽略它注册为一个区域的路线的事实,我也得到一个黄色的错误页面,上面写着:

 视图索引或它的主人没有被发现或没有视图引擎
支持搜索位置。在以下地点被搜查:
〜/查看/ DefaultController / Index.cshtml
〜/查看/ DefaultController / Index.vbhtml
〜/查看/共享/ Index.cshtml
〜/查看/共享/ Index.vbhtml
〜/查看/ DefaultController /的Index.aspx
〜/查看/ DefaultController / Index.ascx
〜/查看/共享/的Index.aspx
〜/查看/共享/ Index.ascx

我需要能够使用的路由处理程序 - 它的 {myRoutVal} 参数一些验证工作,并重定向到一个错误页面,如果它是无效的<。 / p>

帮助!


解决方案

发现问题。我们有 AreaRegistrationContext ,你看到上面的( context.MapRoute )引用的扩展方法 - 这种方法,我未正确设置 route.DataTokens [区] 值,所以当它试图找到该视图,它看起来并不在该地区。

添加下面一行到我的 AreaRegistrationContext 扩展方法解决了这个问题:

  route.DataTokens [区] = context.AreaName;

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!!

解决方案

Found the problem. We have an extension method for AreaRegistrationContext that you see referenced above (context.MapRoute) - in that method, I am not setting the route.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:

route.DataTokens["Area"] = context.AreaName;

这篇关于MVC3 RouteHandler结果忽略了解决途径区 - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆