【问题标题】:Default-Route for root of application应用程序根目录的默认路由
【发布时间】:2016-06-23 01:18:32
【问题描述】:

如何告诉我的mvc-应用程序在未指定时路由到特定的ControllerAction

调试时http://localhost:54500/ 应该路由到http://localhost:54500/Home/Index

目前我有:

routes.MapRoute(
    name: "Root",
    url: "",
    defaults: new { controller = "Home", action = "Index" }
    );

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { id = UrlParameter.Optional }
    );

但这总是抛出

未找到视图“索引”或其主视图,或者没有视图引擎支持搜索到的位置

编辑#1

它应该重定向/路由到位于Area 中的视图,称为Home。只是想澄清一下,有一个Controller 和一个Area,它们都被命名为Home

该区域的配置是:

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

【问题讨论】:

  • 您的项目中的 Views/Home 文件夹下是否有名为 Index.cshtml 的视图?
  • 调用http://localhost:54500/Home/Home/Index 工作正常。
  • 这意味着您的文件夹层次结构错误。应该是Views/Home/Index.cshtml

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


【解决方案1】:

调试时http://localhost:54500/ 应该路由到http://localhost:54500/Home/Index

实际上,按照您的配置方式,http://localhost:54500/ 将路由到 HomeController.Index 方法,而不是另一个 URL。

未找到视图“索引”或其主视图,或者没有视图引擎支持搜索到的位置

此错误表示路由成功,但控制器返回的视图路径不存在。

由于您还提到您正在使用区域并发布了您的配置,所以很清楚发生了什么。您的配置按以下顺序运行:

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

routes.MapRoute(
    name: "Root",
    url: "",
    defaults: new { controller = "Home", action = "Index" }
    );

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { id = UrlParameter.Optional }
    );

因此,如果您传递 URL http://localhost:54500/,Area 路由将丢失(因为它不以 /Home 开头)并且它将匹配 Root 路由。这条Root 路由不会路由到您的区域。有两种方法可以解决此问题。

选项 1 - 将根路由添加到主区域

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "Root",
        "",
        new { controller = "Home", action = "Index" }
        );

    context.MapRoute(
        "Home_default",
        "Home/{controller}/{action}/{id}",
        new {action = "Index", id = UrlParameter.Optional}
        );
}

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { id = UrlParameter.Optional }
    );

选项 2 - 设置 DataToken 以指示 Home Area

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

routes.MapRoute(
    name: "Root",
    url: "",
    defaults: new { controller = "Home", action = "Index" }
    ).DataTokens["area"] = "Home";

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { id = UrlParameter.Optional }
    );

【讨论】:

  • Areas 可能有问题?
  • 可能,但除非您发布整个路由配置(按顺序),否则无法判断哪个路由首先匹配。
  • 您的区域路由配置在哪里?您提到您正在使用区域。您是否也在使用属性路由?解决这个问题需要考虑更多的路由配置。第一场比赛获胜,而不是最好的比赛。因此,要首先查看匹配的内容,我们需要查看整个配置。
  • 请查看我更新的问题。而且,不,目前没有AttributeRouting 设置
  • 选择Option #2,因为这更适合我的需求,并且在以后添加越来越多的Areas 时感觉像是一个很好的解决方案,而无需考虑默认(-root)路由。谢谢!
【解决方案2】:

在 Core 1.0.1 中,您可以在 Startup.cs 中执行此操作:

 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            app.UseMvc(routes =>
            {
                routes.MapRoute(name: "areaRoute",
                 template: "{area:exists}/{controller=Home}/{action=Index}");

                routes.MapRoute(
                    name: "default",
                   // template: "{controller=Home}/{action=Index}");
                   template: "{area=MyArea}/{controller=Home}/{action=Index}");
            });
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-22
    • 1970-01-01
    • 2013-10-01
    • 2015-03-06
    • 1970-01-01
    相关资源
    最近更新 更多