带可选参数的mvc属性路由

时间:2014-11-29 01:28:52

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

我得到了以下属性集

    [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
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}

1 个答案:

答案 0 :(得分:0)

选项路由参数应该是最后一个。在您的情况下,如果您未通过{name},则该路线将与" {country} / {state} / {city} / listing / {address} / {id}"不匹配。然后没有路由匹配第二个网址,所以它会给你404

相关问题