【问题标题】:mvc attribute routing with optional parameter带有可选参数的 mvc 属性路由
【发布时间】:2014-11-29 01:28:51
【问题描述】:

我设置了以下属性

    [Route("{country}/{state}/{city}/listing/{name?}/{address}/{id}", Name = "ViewListing")]
    public ActionResult Index()
    {
    ...
    }

以下网址可以正常工作

http://localhost:16949/ca/on/london/listing/something/1830-avalon-street/d46fc94f-9a90-460a-b18f-7443db3c1897

但是下面给出了 404,即使 name 参数设置为可选

http://localhost:16949/ca/on/london/listing/1830-avalon-street/d46fc94f-9a90-460a-b18f-7443db3c1897

我还定义了其他路由,它们具有相同数量的不同结构的参数,并且这些路由也不会触发(它们也不应该触发),为清楚起见,这里是其他路由

CITY
{country}/{state}/{city}
{country}/{state}/{city}/{type}
{country}/{state}/{city}/bedrooms/{parameters}
{country}/{state}/{city}/{type}/bedrooms/{parameters}
{country}/{state}/{city}/area/{area}
{country}/{state}/{city}/area/{area}/bedrooms/{parameters}

【问题讨论】:

    标签: asp.net-mvc routes custom-attributes


    【解决方案1】:

    选项路由参数应该在最后。在您的情况下,当您没有通过 {name} 时,路线将不匹配“{country}/{state}/{city}/listing/{address}/{id}”。然后不匹配第二个 url,所以它会给你 404

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-18
      • 2013-05-07
      • 2017-07-14
      • 2011-04-08
      • 2011-06-30
      • 2014-12-07
      相关资源
      最近更新 更多