MVC路由与区域匹配控制器:多种类型的发现匹配控制器 [英] MVC Routing with Matching Controllers in Areas: Multiple types were found that match the controller

查看:127
本文介绍了MVC路由与区域匹配控制器:多种类型的发现匹配控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个方面的MVC应用程序。每个区域,以及根,具有HomeController的。

I have an MVC application with a couple of areas. Each of the areas, as well as the root, has a HomeController.

/Home/Index
/Admin/Home/Index
/Content/Home/Index

所有这些做工精细,正确确定要使用的家居控制器。

All of these work fine and properly determine which home controller to use.

我的问题是每个区域还具有ReportsController,但根没有。

My problem is that each of the areas also has ReportsController, but the root does not.

/Admin/Reports/Index
/Content/Reports/Index

如预期这两个工作,但我得到的多类型被发现匹配控制器为网址/报告/索引错误。它匹配的缺省路由,而是蔓延到的区域。我的假设是,它应该404,以同样的方式将路由所有地区失踪呢。

Both of these work as expected, but I get the "Multiple types were found that match the controller" error for the url /Reports/Index. It's matching on the default route but spilling over to the areas. My assumption would be that it should 404, the same way going to a route missing from all areas does.

我的路由配置如下:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    namespaces: new[] { "Website.Controllers" }
);

context.MapRoute(
    "Content",
    "Content/{controller}/{action}/{id}",
    new { action = "Index", controller = "Home", id = UrlParameter.Optional },
    namespaces: new[] { "Website.Areas.Content.Controllers" }
);

context.MapRoute(
    "Admin",
    "Admin/{controller}/{action}/{id}",
    new { action = "Index", controller = "Home", id = UrlParameter.Optional },
    namespaces: new[] { "Website.Areas.Admin.Controllers" }
);

问题路线(/报表/索引)被匹配默认路线地图上,但不是限制于所定义的Website.Controllers它也期待在的区域的名称空间。相反,它应该限制其搜索Website.Controllers并返回404时,它没有找到一个ReportsController那里。

The problem route (/Reports/Index) is matching on the Default route map, but rather than being limited to the defined namespace of Website.Controllers it's also looking in the areas. Instead, it should limit its search to Website.Controllers and return 404 when it doesn't find a ReportsController there.

推荐答案

由于在下面的帖子所述,命名空间是优先的,但可以通过禁用回退查找如下进行限制:

As outlined in the following post, namespaces are for prioritization, but can be made restrictive by disabling the fallback lookup as follows:

routes.MapRoute(...)DataTokens [UseNamespaceFallback] = FALSE;

HTTP://bubblogging.word$p $ pss.com / 2012/06/09 / MVC路由的命名空间/

这篇关于MVC路由与区域匹配控制器:多种类型的发现匹配控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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