ASP.NET-MVC – MVC handler, optional parameters for unknown quantity

I am using MVC routing, which will get an unknown number of parameters at the end of the URL. Something like this:

domain.com/category/ keyword1/keyword2/…/keywordN

These keywords are the values ​​of filters that we must match.

So far, the only way I can think of is UGLY… just Create an ActionResult with more parameters than I might need:

ActionResult CategoryPage(string urlValue1, string urlValue2, string urlValue3, etc…)
{
}

This didn’t feel right. I thought I could squeeze them into a query string, but then I lost the sexy MVC URL, right? Is there a better way to declare a handler method so that it handles an unknown number of optional parameters?

Routing must be connected on Application Start, which should not be that difficult. The maximum number of keywords can be easily determined from the database, so there is no big problem.

Thank you!

You can use catch-all parameters like this:

routes.MapRoute("Category", "category/{*keywords}", new {controller = "Category", action = "Search", keywords = "" });

Then, there will be a parameter in your search operation method:

public ActionResult Search(string keywords)
{
// Now you have to split the keywords parameter with'/' as delimiter.
}

The following is a list of possible URLs that contain the value of the keywords parameter:

http:// www.example.com/category(keyword: “”)
http://www.example.com/category/foo(keyword: “foo”)
http://www.example.com /category/foo/bar(keyword: “foo / bar”)
http://www.example.com/category/foo/bar/zap(keyword: “foo / bar / zap”)

I am using MVC routing, which will get an unknown number of parameters at the end of the URL. Something like this:

domain.com/ category/keyword1/keyword2/…/keywordN

These keywords are the values ​​of the filter that we must match.

So far, the only way I can think of is UGLY… Just create an ActionResult with more parameters than I might need:

ActionResult CategoryPage(string urlValue1, string urlValue2, string urlValue3, etc…)
{
}

This doesn’t feel right. I think I can change They were stuffed into a query string, but then I lost the sexy MVC URL, right? Is there a better way to declare a handler method so that it handles an unknown number of optional parameters?

Routing must be connected on Application Start, which should not be that difficult. The maximum number of keywords can be easily determined from the database, so there is no big problem.

Thank you!

You can use catch-all parameters like this:

routes.MapRoute( "Category", "category/{*keywords}", new {controller = "Category", action = "Search", keywords = "" });

Then, your search operation method will There is a parameter:

public ActionResult Search(string keywords)
{
// Now you have to split the keywords parameter with'/' as delimiter .
}

The following is a list of possible URLs, which contain the value of the keywords parameter:

http://www.example.com/category(keyword: ” “)
http://www.example.com/category/foo(keyword: “foo”)
http://www.example.com/category/foo/bar(keyword: “foo /bar”)
http://www.example.com/category/foo/bar/zap(keyword: “foo / bar / zap”)

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2105 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.