我在 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
没有被搜查。
我该如何解决这个问题?