【问题标题】:ASP.Net MVC 6 route errorASP.Net MVC 6 路由错误
【发布时间】:2016-07-30 14:24:49
【问题描述】:

我有 ASP.Net MVC 6 应用程序

我添加了如下路线:

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}",
        defaults: new {Controllers="Statics", action="Index"}
        );

});

但我得到如下错误:

System.InvalidOperationException:路由参数“控制器” 同时指定了内联默认值和显式默认值。 当显式指定默认值时,路由参数不能包含内联默认值,请考虑删除其中一个。

有什么建议吗?

【问题讨论】:

  • 删除默认值或内联默认值

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


【解决方案1】:

问题是您试图同时定义内联默认值和显式默认值。在定义template 参数时,= 运算符将分配defaults,但您随后尝试在下一行显式定义默认值。考虑以下几点:

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "default",
        // Notice the removal of the defaults from the template?
        template: "{controller}/{action}/{id?}",
        defaults: new {Controllers="Statics", action="Index"}
        );

});

另一种方法是使用内联 defaults 定义 template,然后像这样完全省略 defaults 行:

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "default",
        // Notice how we assign the defaults inline, and omit the defaults line?
        template: "{controller=Statics}/{action=Index}/{id?}"
        );    
});

【讨论】:

    【解决方案2】:

    错误信息告诉你需要做什么

    您可以删除内联默认值

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller}/{action}/{id?}",
            defaults: new {controller="Statics", action="Index"}
            );
    
    });
    

    或者去掉显式的默认值模板:

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Statics}/{action=Index}/{id?}"                
            );
    });
    

    【讨论】:

    • 我在控制器中创建了一个标题为 Statics 的文件夹,我在其中放置了一些静态控制器,如何才能使用这些控制器功能
    • 不管你把它们放在哪里。一旦他们遵循您在路线中设置的约定。
    • 此答案解决了您原始帖子中所述的错误。
    【解决方案3】:

    经过搜索和测试发现了一个不错的帖子:http://stephenwalther.com/archive/2015/02/07/asp-net-5-deep-dive-routing

    我像下面这样更新了我的路线,效果很好

                app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
    
                routes.MapRoute(
                  name: "route2",
                  template: "statics",
                  defaults: new { controller = "Departments", action = "Index" }
    
                 );
    
                routes.MapRoute(
                   name: "route3",
                   template: "statics/SYears",
                   defaults: new { controller = "SYears", action = "Index" }
                );
                /*
                  routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}",
                    defaults: new {controller="Statics", action="Index"}
                    );
    
                */
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-08
      • 2020-04-04
      相关资源
      最近更新 更多