2

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?

8
  • Using query string parameters is more suitable in this case. And also u can keep using the SearchViewModel like this public async Task<IActionResult> Display([FromQuery] SearchViewModel viewModel) Commented May 29, 2021 at 18:50
  • See following : stackoverflow.com/questions/35296018/… Commented May 29, 2021 at 18:51
  • @Eldar query parameters are not SEO friendly. The URL in the question will provide a natural URL that is SEO friendly Commented May 29, 2021 at 19:03
  • @jdweng I don't think having multiple route is doable as there are lots of combination these filters. For example, 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 available Commented May 29, 2021 at 19:13
  • @Jay then you need a something like a capture-all kind of route and a regex processing of route parameters. Like this. It is for old asp.net but it can give you the idea Commented May 29, 2021 at 19:20

1 Answer 1

1

Using a route definition like this will make it capture all those routes you provided :

[Route("homes-for-sale-in-{city}-{id}/{**catchAll}")]
[HttpGet]
public async Task<IActionResult> City(string city, string id, string catchAll)
{
  // Here you will parse the catchAll and extract the parameters        
  await Task.Delay(100);
  return this.Ok(catchAll);
}

Also please note that catchAll parameter can't be made optional. So a request like /homes-for-sale-in-los-angeles-100/ will result in 404.

Sign up to request clarification or add additional context in comments.

4 Comments

This does not work. Its is throwing the following error RoutePatternException: A path segment that contains more than one section, such as a literal section or a parameter, cannot contain a catch-all parameter.
Actually this code worked! Thank you so much!!
@Jay I have the same error message. How'd you solve for that?
Just kidding--I see, you just cannot have a segment after the catch-all...which makes sense!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.