Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 104479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:17:17+00:00 2026-05-11T01:17:17+00:00

Here is my scenario. For the example lets say that I need to return

  • 0

Here is my scenario. For the example lets say that I need to return a list of cars based on a search criteria. I would like to have a single View to display the results since the output will be the same, but I need several ways of getting there. For instance, I may have a Form with a textbox to search by year. I may have another separate page that contains a hyperlink for all red, Toyota cars. How do I handle these multiple scenarios in the same View and Controller. My dilemma is that the search could contain several options… year, make, model, etc but I don’t know where to put them.

What is the best approach for this? Should I define the parameters in the routing or go with query strings, etc?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:17:18+00:00Added an answer on May 11, 2026 at 1:17 am

    Option 1

    Of course you always can choose the way of /car/search/?vendor=Toyota&color=Red&model=Corola and I think it will be good for you.

    routes.MapRoute(     'CarSearch',     'car/search',     new { controller = 'car', action = 'search' } ); 

    You can get params from Request.Params in action in this case.

    Option 2

    Or you can define params in the routing table, but AFAIK it will be required to make a set of rules for all possible combinations, because an order of the params matter, for example:

            routes.MapRoute(             'CarSearch1',             'car/search/vendor/{vendor}/color/{color}/model/{model}',             new {controller = 'car', action = 'search'}         );          routes.MapRoute(             'CarSearch2',             'car/search/color/{color}/vendor/{vendor}/model/{model}',             new {controller = 'car', action = 'search'}         );          routes.MapRoute(             'CarSearch3',             'car/search/model/{model}/color/{color}/vendor/{vendor}',             new {controller = 'car', action = 'search'}         ); 

    … an so on. It’s true if you are going with the standard MvcRouteHandler.

    But it was an easy ways 🙂

    Option 3

    The hard, but, I think, most elegant way, is to make your own IRouteHandler implementation – it will give you much more flexibility in params order. But again, its a hard way, dont go with it if you have a simple app. So, just for example of how to make it this way (very simple example):

    Add new route to the list of routes:

    routes.Add     (         new Route             (                 'car/search/{*data}',                 new RouteValueDictionary(new {controller = 'car', action = 'search', data = ''}),                 new MyRouteHandler()             )     ); 

    Add classes that will tweak the standard request processing chain:

    class MyRouteHandler : IRouteHandler {     public IHttpHandler GetHttpHandler(RequestContext requestContext)     {         return new MyHttpHandler(requestContext);     } }  class MyHttpHandler : MvcHandler {     public MyHttpHandler(RequestContext requestContext) : base(requestContext)     {     }      protected override void ProcessRequest(HttpContextBase httpContext)     {         IController controller = new CarController();         (controller as Controller).ActionInvoker = new MyActionInvoker();         controller.Execute(RequestContext);     } }  class MyActionInvoker : ControllerActionInvoker {     protected override ActionResult InvokeActionMethod(MethodInfo methodInfo, IDictionary<string, object> parameters)     {         // if form of model/{model}/color/{color}/vendor/{vendor}         var data = ControllerContext.RouteData.GetRequiredString('data');         var tokens = data.Split('/');          var searchParams = new Dictionary<string, string>();         for (var i = 0; i < tokens.Length; i++)         {             searchParams.Add(tokens[i], tokens[++i]);         }          parameters['searchParams'] = searchParams;          return base.InvokeActionMethod(methodInfo, parameters);     } } 

    In controller:

    public ActionResult Search(IDictionary<string, string> searchParams) {     ViewData.Add         (             // output 'model = Corola, color = red, vendor = Toyota'             'SearchParams',             string.Join(', ', searchParams.Select(pair => pair.Key + ' = ' + pair.Value).ToArray())         );     return View(); } 

    And it will work with any search parameters order:

    /car/search/vendor/Toyota/color/red/model/Corola /car/search/color/red/model/Corola/vendor/Toyota /car/search/model/Corola/color/red/vendor/Toyota 

    But also dont forget to make a link generation logic, because Html.ActionLink and Html.RenderLink will not give you url in pretty form of /car/search/model/Corola/color/red/vendor/Toyota, so you’ll need to make a custom link generator.

    So, if you need a really flexible routing – you’d better go with this hard way 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 77k
  • Answers 77k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer When you post back, you'll have to rebind the data… May 11, 2026 at 3:22 pm
  • added an answer http://code.google.com/p/subsonicproject/issues/list Make sure it's still a bug in 2.2. Edit:… May 11, 2026 at 3:22 pm
  • added an answer I would inherit from Control and create your own as… May 11, 2026 at 3:22 pm

Related Questions

I ran into a scenario where LINQ to SQL acts very strangely. I would
I recently asked a question about functional programming, and received (good!) answers that prompted
Surprisingly I was only able to find one previous question on SO about this
I know that XHTML doesn't support nested form tags and I have already read

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.