MVC区域主页显示默认主页

时间:2016-06-16 21:09:03

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

我最近在现有的MVC 4网络应用中添加了区域。我的一个区域有一个Home控制器,所以很明显当我导航到 / MyArea / Home / Index 时,我想显示它的索引视图。最初我收到以下错误:

  

发现多个类型与名为' Home'的控制器匹配。如果为此请求提供服务的路由(' {controller} / {action} / {id}')未指定名称空间来搜索与请求匹配的控制器,则会发生这种情况。如果是这种情况,请通过调用' MapRoute'的过载来注册此路线。采用'命名空间的方法'参数。

     

' Home'找到了以下匹配的控制器:   MyApp.Controllers.HomeController   MyApp.Areas.MyArea.Controllers.HomeController

研究这个问题我发现我需要在调用 MapRoutes()时添加默认命名空间,并且它已经停止了错误。不幸的是,我现在发现,当我转到 / MyArea / Home / Index 时,应用实际上会显示 / Home / Index 的视图 - 我无法显示操作来自Area的Home控制器。

这是我的代码:

Global.aspx.cs

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    WebApiConfig.Register(GlobalConfiguration.Configuration);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

MyApp.RouteConfig

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

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

MyApp.Areas.MyArea.MyAreaRegistration

public override void RegisterArea(AreaRegistrationContext context)
{
    var ns = typeof(MyApp.Areas.MyArea.Controllers.HomeController).Namespace;

    var routeName = AreaName + "_Route";
    var routeUrl = AreaName + "/{controller}/{action}/{id}";
    var routeDefaults = new {controller = "Home", action = "Index", id = UrlParameter.Optional};
    var routeNamespaces = new[] {ns};

    context.MapRoute(
        name: routeName,
        url: routeUrl,
        defaults: routeDefaults,
        namespaces: routeNamespaces
        );
}

有人对如何解决这个问题有任何好主意吗?

更新

仅在调用HomeController类(如Index)中存在的操作时才会出现此问题。在区域的HomeController中调用默认HomeController中不存在的操作会显示正确的视图。

更新2:

原来这是一个经典的PICNIC错误 - 我的代码中只是一个简单的拼写错误,因此该操作会寻找一个不存在的视图。因此,它可以找到它可以找到的第一个匹配视图 - " root"

中的一个

3 个答案:

答案 0 :(得分:1)

当你链接到MyArea区域中的那个时,你需要在routeValues参数中添加new { Area = "MyArea" }

答案 1 :(得分:1)

+(NSString *) findAllMessages{ __block NSMutableArray *totalMessages = [[NSMutableArray alloc] init]; __block NSDictionary *values; __block NSString *concatenatedMessage; FIRUser *current_user = [[FIRAuth auth] currentUser]; FIRDatabaseReference *dbReference = [[[[FIRDatabase database] reference] child:@"Messages"] child:current_user.uid]; FIRDatabaseQuery *allMessages = [dbReference queryOrderedByChild:current_user.uid]; [allMessages observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) { NSMutableDictionary *eachMessage = snapshot.value; concatenatedMessage = @""; for(id key in eachMessage){ [totalMessages addObject:key]; values = [eachMessage objectForKey:key]; for(id key2 in values){ concatenatedMessage = [concatenatedMessage stringByAppendingString:[values objectForKey:key2]]; concatenatedMessage = [concatenatedMessage stringByAppendingString:@"^^"]; } concatenatedMessage = [concatenatedMessage stringByAppendingString:@"%%"]; } }]; NSLog(@"%@",concatenatedMessage); NSString *newString = concatenatedMessage; return newString; } 功能中,您应该更新RegisterArea以包含区域名称。

routeDefaults

答案 2 :(得分:0)

<捂脸>

在导入区域代码的过程中,我在视图的文件夹名称中输入了一个拼写错误,因此MVC无法为区域操作找到正确的index.cshtml。

< /捂脸>

从外观上看,当MVC无法找到区域操作的视图时,它会使用默认视图。一旦我修复了Areas \ MyArea \ Views中文件夹的名称以匹配控制器名称,一切都按预期工作。

+1给所有回复的人 - 您的回复很有帮助且我已在我的更新代码中使用了