多参数路由不匹配

时间:2016-09-11 04:13:31

标签: asp.net asp.net-mvc asp.net-core

有人可以向我解释为什么这条路线:

routes.MapRoute(
    "ListingDetails",
    "{city}-{propertyType}-for-sale-MLS-{mlsId}",
    new {controller= "Search", action="Details"}
    );

与此操作不匹配:

public async Task<IActionResult> Details(int mlsId, string city, string propertyType, string mls)
        {
            var listing = _listingService.GetListingByMlsId(mlsId);
            return View(listing);
        }

我已经将路线列为第一个路线,因此在捕获路线之前没有路线。而不是它匹配我只是得到搜索/详细信息?mlsId = ...&amp; propertyType = ...

1 个答案:

答案 0 :(得分:1)

mls参数是罪魁祸首。您应该能够通过为mls参数指定默认值来修复它:

public async Task<IActionResult> Details(... string mls = "something")

或者在路线中定义默认值:

new {controller= "Search", action="Details", mls="something"}