问题描述
我在MVC 5中遇到了区域路由问题.当我浏览到/Evernote/EvernoteAuth时,出现404资源找不到错误.
I am having a problem with an Area route in MVC 5. When I browse to /Evernote/EvernoteAuth I get a 404 resource cannot be found error.
我的区域看起来像这样:
My area looks like this:
Areas
Evernote
Controllers
EvernoteAuthController
Views
EvernoteAuth
Index.cshtml
EvernoteAreaRegistration.cs(更新:未调用RegisterArea(),所以我进行了清理并重建".现在被调用,但结果相同.)包含以下路由图:
The EvernoteAreaRegistration.cs (UPDATE: RegisterArea() wasn't being called so I did a Clean and Rebuild. Now it is called but same result.) contains this route map:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Evernote_default",
"Evernote/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
EvernoteAuthController的Index()方法仅返回View().
The EvernoteAuthController's Index() method simply returns View().
我的应用程序的RouteConfig.cs当前未定义路由映射,但是我在此处尝试通过实现以下方式强制"使用它:
My application's RouteConfig.cs currently has no route maps defined, but I tried manually "forcing" it here by implementing this:
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" }
);
但是无论该路线图是否存在或被注释掉,我都得到相同的结果.
but I get the same results whether this route map exists or is commented out.
使用Phil Haack的 asp.net mvc路由调试器,我看到了我的路由很好,区域名称,控制器名称和操作方法名称也匹配.我在控制器操作方法中设置了断点,但从未输入这些方法.更新:浏览到/Evernote/EvernoteAuth时从未输入这些方法,但是当我仅浏览到区域名称/Evernote时,实例化了EvernoteAuthController并调用了Index()方法. (为什么控制器由/Evernote而不是/Evernote/EvernoteAuth实例化?)然后我收到错误消息:
Using Phil Haack's asp.net mvc routing debugger I saw that my routes matched fine and the area name, controller name and Action method names matched. I put breakpoints in the controller action methods and those methods were never entered. UPDATE: Those methods were never entered when browsing to /Evernote/EvernoteAuth however when I browsed to just the area name, /Evernote, an EvernoteAuthController was instantiated and the Index() method was called. (Why is that controller being instantiated by /Evernote by not by /Evernote/EvernoteAuth?) Then I received the error:
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
.
In this case I believe ~ = / (application root). So the area Areas\Evernote\Views
is not being searched.
如何解决此问题?
推荐答案
将正确的名称空间添加到控制器很重要
It is important tha you add the correct namespace to your controller
namespace YourDefaultNamespace.Areas.Evernote.Controllers
{
public class EvernoteAuthController : Controller
{
...
...
}
}
因此路由可以找到您的控制器.现在,您必须使用方法
So the routing can find your controller.Now you have to register the area in the Global.asax.cs with the method
AreaRegistration.RegisterAllAreas();
这篇关于使用ASP.NET MVC区域路由找不到错误404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!