MVC区域 - 控制器不返回视图

时间:2016-11-11 12:45:40

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

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

  

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

这是MyAreaAreaRegistration:

Timestamp timestamp = new Timestamp(example.getExampleDate().getTime());
timestamp.setNanos(3);
stmt.setTimestamp(i++, timestamp);

Routeconfig:

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

的Global.asax:

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" }
            );

和控制器:

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

我完全坚持这一个。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

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

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"); ;

这对你有用。

谢谢。

相关问题