我在MVC 5中遇到了一个区域路由问题。当我浏览到/Evernote/EvernoteAuth时,会出现404资源找不到的错误。
我的区域如下:
Areas
Evernote
Controllers
EvernoteAuthController
Views
EvernoteAuth
Index.cshtml
EvernoteAreaRegistration.cs文件包含以下路由映射(更新:RegisterArea()未被调用,因此我执行了清理和重新构建。现在已被调用,但结果相同。):
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Evernote_default",
"Evernote/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
EvernoteAuthController的Index()方法只返回View()。我的应用程序的RouteConfig.cs目前没有定义路由映射,但我在这里尝试手动“强制”实现它,如下所示:
routes.MapRoute(
name: "EvernoteAuthorization",
url: "Evernote/{controller}/{action}",
defaults: new { controller = "EvernoteAuth", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "AysncOAuth.Evernote.Simple.SampleMVC.Controllers" }
);
无论这个路由图是否存在或被注释掉,我得到的结果都是一样的。
使用Phil Haack的asp.net mvc路由调试器,我发现我的路由匹配得很好,区域名、控制器名和操作方法名都匹配。我在控制器操作方法中设置了断点,但那些方法从未被执行。更新:当我浏览到/Evernote/EvernoteAuth时,那些方法从未被执行;然而,当我浏览到仅区域名称/Evernote时,一个EvernoteAuthController被实例化并调用Index()方法。(为什么该控制器被/Evernote实例化而不是/Evernote/EvernoteAuth?) 然后我收到了以下错误消息:
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/EvernoteAuth/Index.aspx
~/Views/EvernoteAuth/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/EvernoteAuth/Index.cshtml
~/Views/Shared/Index.cshtml
and so on...
在这种情况下,我相信 ~ = /(应用程序根目录)。因此,区域Areas\Evernote\Views
没有被搜索到。我该如何进行故障排除?