不在区域文件夹中搜索视图

Not searching for view in Areas folder

我是新手,如有遗漏请见谅。

我正在尝试访问名为 "Birthday" 的页面,其中有一个视图 "Birthday.cshtml"。我的 url 看起来像 http://example.com/en-us/Event/Birthday。当我尝试转到 url 时,浏览器显示此错误:

The view 'Birthday' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Event/Birthday.aspx
~/Views/Event/Birthday.ascx
~/Views/Shared/Birthday.aspx
~/Views/Shared/Birthday.ascx
~/Views/Event/Birthday.cshtml
~/Views/Event/Birthday.vbhtml
~/Views/Shared/Birthday.cshtml
~/Views/Shared/Birthday.vbhtml

我一辈子都弄不明白为什么它不在 ~/Areas/Event/Views/Birthday.cshtml 位置。

我的文件夹结构如下:

Areas
    \ Event
        \ Controllers
            - EventController.cs
        \ Models
        \ Views
            - Birthday.cshtml
- EventAreaRegistration.cs

EventAreaRegistration 看起来像这样:

public class EventAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Event";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Event_default",
            "Event/{action}/{id}",
            new { controller = "Event", action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { "ProjectName.Areas.Event.Controllers" }
        );
    }
}

Global.asax.cs 包括这些行:

AreaRegistration.RegisterAllAreas();

RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);

RegisterRoutes 看起来像这样:

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default",
            "{locale}/{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", locale = Locale.defaultLocale, id = UrlParameter.Optional }
        );
    }

有什么想法吗?

想通了。 URL 的 {locale} 部分在 EventAreaRegistration.cs MapRoute() 函数中丢失,因此该路由实际上并未被使用。

public class EventAreaRegistration : AreaRegistration 
{
    public override string AreaName
    {
        get
        {
            return "Event";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Event_default",
            "{locale}/Event/{action}/{id}",
            new { controller = "Event", action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { "ProjectName.Areas.Event.Controllers" }
        );
    }
}
Change it to
public class EventAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Event";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Event_default",
            "Event/{controller }/{action}/{id}",   //change here add controller
            new { controller = "Event", action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { "ProjectName.Areas.Event.Controllers" }
        );
    }
}