【问题标题】:Wilcard routing to methods with params in mvc 6通配符路由到 mvc 6 中带有参数的方法
【发布时间】:2015-11-21 13:15:24
【问题描述】:

我正在尝试在测试 MVC 6 Web API 项目中创建通配符路由。但我不知道该怎么做。我目前在我的Controller 课程中有这个。

    // GET: api/values
    [HttpGet("{*anything:regex(^(.*)?$)}")]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

    // GET api/values/5
    [HttpGet("{*anything}/{id}")]
    public string Get(int id)
    {
        return "value";
    }

如果我的Controller 中只有第一个Get(),那么它可以工作。但是,当我添加第二个Get(int id) 方法时,我会得到一个Internal Server Error。事实上,正是[HttpGet("{*anything}/{id}")] 行导致了上述第二个Get 方法的问题。

我该如何解决这个问题?

【问题讨论】:

    标签: asp.net-web-api asp.net-web-api2 asp.net-core-mvc


    【解决方案1】:

    事实上,是 [HttpGet("{*anything}/{id}")] 行导致 第二种Get方法上面的麻烦。

    据我所知,通配符段 ({*anything}) 只能是路由模板的最后一段。

    因此,您不能在 {*anything} 之后使用带有 {id} 段的模板。

    我建议你在你的应用程序中enable logging,那里显示最多的错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-22
      • 2023-03-30
      • 1970-01-01
      • 2017-11-03
      • 1970-01-01
      • 2012-05-10
      相关资源
      最近更新 更多