0

My problem is that I give to my action 3 parameters (category, city, page) and some of them could be null because I need to make 3 filterings:

  • one by category (category != null && city == null)
  • one by city (category == null && city != null)
  • one by both of them (category != null && city != null)

My problem is on routing. When (category != null && city == null) it doesn't work. It gives to category parameter from my action null value, and my city parameter receives the category's value.

My Global.asax:

routes.MapRoute(
            "ListByCity",
            "Advertisers/{city}/{page}",
            new { controller = "Advertisers", action = "List"  }
            );

        routes.MapRoute(
            "ListByCategory",
            "Advertisers/{category}/{page}",
            new { controller = "Advertisers", action = "List" }
            );

        routes.MapRoute(
            "List",
            "Advertisers/{category}/{city}/{page}",
            new { controller = "Advertisers", action = "List" }
            );

Please help me.

1
  • 1
    use query string instead Commented Dec 3, 2010 at 13:23

4 Answers 4

1

think the problem in the opposite way. If you have the URL http://YourServer/Advertisers/Text How would you know if that text is a category or a city? You can resolve the matching problem with a regular expression but both cities and categories are strings so you have no way of telling the routing system which one to match. You will have to differenciate them. Maybe creating a route that matches /Advertisers/Categories/{category} and other that matches /Advertisers/Cities/{city}.

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

1 Comment

Resolving the matching problem with a regular expression could work if I include my cities/categories from my database. It might work.
1

That looks more like three separate actions to me:

/Advertisers/List/{category}/{city}/{page}
/Advertisers/ListByCity/{city}/{page}
/Advertisers/ListByCategory/{category}/{page}

These can all call a common method in your controller to prepare a model for your List view.

EDIT:

Or you need to add a category called "all" and a city called "all" and then you can get away just one route:

/Advertisers/List/{category}/{city}/{page}

2 Comments

I agree with you but I don't need my action name on url.
Why not? It's the default way of doing it: {controller}/{action}/{parameter}. Besides, I don't think what you want to do is really possible without using query strings or including the type of search in the URL as a parameter. Or change the order of parameters (move {page} to the front) and then figure out which type of parameter(s) you have in your controller before generating your list.
0

Its best to use querystrings.

Please refer to https://stackoverflow.com/questions/4.....

It has quite a few good suggestions too.

Comments

0

I agree with @MCL's edit.. I think you can take that approach without having an action as well.

/Advertisers/All/NewYork/1
/Advertisers/SomeCategory/NewYork/2

You setup your route something like this:

routes.MapRoute(
        "List",
        "Advertisers/{category}/{city}/{page}",
        new { controller = "Advertisers", action = "List" });

And your Action would look something think this:

public ActionResult List ( string category, string city, int page ) { .. }

I also disagree with query strings in this case. Its easier, sure, but I feel like this URI pattern would be a core part of your app and not setting up a proper route system for it would hinder you in the long run.

Comments

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.