在ASP.NET MVC区域路由中找不到错误404

时间:2014-08-22 15:27:53

标签: asp.net asp.net-mvc asp.net-mvc-routing

我遇到了MVC 5中的区域路由问题。当我浏览到/ Evernote / EvernoteAuth时,我找不到404资源错误。

我的区域如下:

Areas
    Evernote
        Controllers
            EvernoteAuthController
        Views
            EvernoteAuth
                Index.cshtml

EvernoteAreaRegistration.cs(UPDATE:RegisterArea()没有被调用所以我做了一个Clean和Rebuild。现在它被调用但结果相同。)包含这个路由图:

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 routing debugger我看到我的路线匹配正常,区域名称,控制器名称和操作方法名称匹配。我在控制器操作方法中放置了断点,并且从未输入过这些方法。更新:浏览/ 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

如何解决此问题?

4 个答案:

答案 0 :(得分:21)

为控制器添加正确的命名空间非常重要

  namespace YourDefaultNamespace.Areas.Evernote.Controllers
  {
    public class EvernoteAuthController : Controller
    { 
        ...
        ...
    }
  }

因此路由可以找到您的控制器。 现在,您必须使用方法

在Global.asax.cs中注册该区域
AreaRegistration.RegisterAllAreas();

答案 1 :(得分:3)

谨慎使用AreaRegistration.RegisterAllAreas();方法中的Application_Start

如果您将AreaRegistration.RegisterAllAreas()置于Application_Start内的最后一个无效的位置。

AreaRegistration.RegisterAllAreas()放在首位,路由将成功执行..

示例:

 protected void Application_Start(object sender, EventArgs e)
 {
        AreaRegistration.RegisterAllAreas();  //<--- Here work

        FilterConfig.Configure(GlobalFilters.Filters);
        RouteConfig.Configure(RouteTable.Routes);

        AreaRegistration.RegisterAllAreas();  //<--- Here not work
 }

答案 2 :(得分:2)

就像您在我http://legacy.piranhacms.org/the-magic-of-mvc-routing-with-multiple-areas的帖子中找到的那样,您可能已经发现所有控制器都映射到默认路由(即您在路由配置中手动添加的路由)。如果它已被添加到默认路由,那么它将在该位置搜索其视图的默认路由,即~/Views/...

因此错误似乎是区域未正确配置。确保Global.asax.xs中包含以下行:

AreaRegistration.RegisterAllAreas();

这是实际设置区域的行,并确保当区域中的控制器被命中时,搜索该区域的视图目录,在您的情况下~/Areas/Evernote/Views。我博客文章中介绍的内容是如何消除Evernote区域中的控制器在默认路由中的映射。

希望这有帮助!

此致

哈坎

答案 3 :(得分:0)

在我的情况下,global.asax.cs的Application_Start中的配置顺序为

AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);

更改订单使其生效。

GlobalConfiguration.Configure(WebApiConfig.Register);
AreaRegistration.RegisterAllAreas();