不包括URL结果区域名称"视图“索引”或它的主人没有被发现"而不是404 [英] Not including area name in URL results in "The view 'Index' or its master was not found" instead of 404

查看:105
本文介绍了不包括URL结果区域名称"视图“索引”或它的主人没有被发现"而不是404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用这个网址:

http://www.domain.com/Dashboard/Admin/Index

我的页面按预期工作。 控制台是一个MVC区,管理​​是控制器和首页认为

My page works as expected. Dashboard is an MVC Area, Admin is the controller and Index is the view.

如果我删除了区域名称,如:

If I remove the area name, like this:

http://www.domain.com/Admin/Index

然后我收到以下错误:

Then I get the following error:

这是唯一一种-的预期;它不应该是一个404?

That's only kind-of expected; shouldn't it be a 404?

为什么MVC仍成功定位控制器,并试图找到视图时,我不指定区域?

如何强制它时,不包括区域返回404?

我试着改变命名空间,但它不会有所作为。

I've tried changing namespaces but it doesn't make a difference.

推荐答案

默认情况下,内置的控制器工厂将找到匹配的控制器的所有实现无论命名空间。而视图引擎将搜索的请求的上下文相关联的视图(即,将不会在一个区域,如果区域路由尚未匹配查询)。总之你需要限制路由引擎定义,只有在'定义'命名空间,那么404将被抛出搜索。

By default the built-in controller factory will find all implementations of matched controllers regardless of namespace. While the view engine will search for the associated view in context of the request (i.e. won't search in an area if the area route has not been matched). In short you need to limit the route engine definitions to only search in 'defined' namespaces, then the 404 will be thrown.

这是通过使用图路线方法的过载,并通过命名空间实现:

This is accomplished by using an overload of the MapRoute method, and passing the namespaces:

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
    null, // object constraints
    new string[] { "Namespace.Application.Controllers" } // namespaces
);

有关实验的目的看控制器工厂确实具有相同名称的多个控制器的,可以尝试增加另一个AdminController到根控制器文件夹,然后运行相同的要求。

For experimental purposes to see what the controller factory does with multiple controllers of the same name, try adding another AdminController to the root Controllers folder and then run the same request.

这篇关于不包括URL结果区域名称"视图“索引”或它的主人没有被发现"而不是404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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