【问题标题】:MVC Areas - Controller does not return viewMVC 区域 - 控制器不返回视图
【发布时间】:2017-03-25 16:55:06
【问题描述】:

我在 MVC 项目中使用区域时遇到了一些问题。我可以访问位于该区域下方的控制器,但是当它返回视图(带模型)时,出现错误:

未找到视图“索引”或其主视图或没有视图引擎 支持搜索的位置。搜索了以下位置: ~/Views/MyController/Index.aspx ~/Views/MyController/Index.ascx 等

这是 MyAreaAreaRegistration:

public override void RegisterArea(AreaRegistrationContext context)
{
     context.MapRoute(
        "MyArea_default",
        "MyArea/{controller}/{action}/{id}",
        new { controller = "MyController", action = "Index", 
        id = UrlParameter.Optional });
}

路由配置:

routes.MapRoute(
                name: "Default", // Route name
                url: "{controller}/{action}/{id}", // URL with parameters
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                namespaces: new[] { "MyApp.Controllers" }
            );

全球.asax:

protected void Application_Start()
{
     AreaRegistration.RegisterAllAreas();
     ...
     RouteConfig.RegisterRoutes(RouteTable.Routes);
}

和控制器:

return View(myViewModel);

我完全被这个卡住了。任何帮助将不胜感激。

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4 asp.net-mvc-areas


    【解决方案1】:

    请更改您的路线配置代码。

    routes.MapRoute(
                     name: "Default", // Route name
                url: "{controller}/{action}/{id}", // URL with parameters
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                ).DataTokens.Add("area", "MyArea"); ;
    

    它会为你工作。

    谢谢。

    【讨论】:

    • 嗨,谢谢,这确实适用于这个区域的这个视图,但是我在区域之外也有视图和控制器(“主应用程序”),这些停止工作了。如果我更改为 DataTokens.Add("area", ""),那么其他人工作,但 area 没有。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多