I have an application that is written in ASP.NET Core 5.0. I have a need to create a flexible route that would bind multiple string parameters.
for example, the route would look like this
/homes-for-sale-in-los-angeles-100/0-5000_price/condo,apartment_type/0-5_beds/0-4_baths/2_page
In the above URL the only required part will be /homes-for-sale-in-los-angeles-100. los-angeles is the city name and 100 is the id. The rest of it are just parameters. The 0-5000_price meaning I want to bind the value 0-5000 to a parameter called price.
Not always all the parameters are provided. Here are some different shapes of the same route
/homes-for-sale-in-los-angeles-100/condo,apartment_type
/homes-for-sale-in-los-angeles-100/0-5000_price/10_page
/homes-for-sale-in-los-angeles-100/condo_type/0-5000_price/2_page
Here is what I have done
[Route("/homes-for-sale-in-{city}-{id:int}.{filter?}/{page:int?}", Name = "HomesForSaleByCity")]
public async Task<IActionResult> Display(SearchViewModel viewModel)
{
return View();
}
public class SearchViewModel
{
[Required]
public int? Id { get; set; }
public string City { get; set; }
public string Price { get; set; }
public string Type { get; set; }
public string Beds { get; set; }
public string Baths { get; set; }
public int Page { get; set; }
}
How can I create a route that would allow multiple optional parameters and bind them correctly?
SearchViewModellike thispublic async Task<IActionResult> Display([FromQuery] SearchViewModel viewModel)Price, Type, and beds,Price and beds,bed and type.... I am looking for more of a robust solution that would just parse all these parameters and populate what is availablecapture-allkind of route and a regex processing of route parameters. Like this. It is for old asp.net but it can give you the idea