Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have the below route setup but it is not coming out the way I am expecting. Yes I am still new to MVC.

The way it is coming out is like this.

http://localhost:29998/Home/States?make=Chrysler

the way I want it to come out is like this

http://localhost:29998/Home/Chrysler/States

Then of course once you click on your state it would look like this.

http://localhost:29998/Home/Chrysler/Florida

I would realy love to be able to remove "home from that altogether and just leave it as

http://localhost:29998/Chrysler/States

routes.MapRoute(
                "States", // Route name
                "{controller}/{action}/{make}", // URL with parameters
                new { controller = "Home", action = "States", Make = "" } // Parameter defaults
            );
share|improve this question
Ok so if I put a URL like this localhost:29998/Home/States?make=Chrysler and I put a break point on the return I see that string make has the value of Chrysler but if I use this URL the string make takes a null value. localhost:29998/Home/States/Chrysler Im sure there is something small I am over looking that I should be doing I just cant seem to think of what it is – scripter78 May 29 '12 at 3:15
'public ActionResult States(string make) { IEnumerable<DealerHolder> stateNames = new List<DealerHolder>(); stateNames = _repository.GetAllDealers(); var states = from state in stateNames where state.make == make orderby state.address.city ascending select state.ToDomainDealer(); return View(states.ToList<DealerHolder>()); }' 'routes.MapRoute( "States", // Route name "{controller}/{action}/{make}", // URL with parameters new { controller = "Home", action = "States", make = UrlParameter.Optional } // Parameter defaults );' – scripter78 May 29 '12 at 3:15
How are you generating the link? – Darin Dimitrov May 29 '12 at 6:25
@Html.ActionLink(item.make, "States", new { make = item.make }) – scripter78 May 29 '12 at 14:38

2 Answers

up vote 1 down vote accepted

Here is your solution:

localhost:29998/Chrysler/States

routes.MapRoute(
                "States", // Route name
                "{make}/{states}/", // URL with parameters
                new { controller = "Home", action = "GetStateData", make="", states="" } 
            );

You should place it below the default routing method so it does not take your controller values.

share|improve this answer
I tried this and it still is not working – scripter78 May 29 '12 at 14:36
it should work place a debug point and check the parameter values. It must be like make = home, state = index. So to avoid this add more routes like "home"/{action}/{id} for ever controller – Imran Rashid May 29 '12 at 16:28

Please refer below url. http://mvccoderouting.codeplex.com/wikipage?title=Hierarchical%20Routes&referringTitle=Documentation

share|improve this answer
Although this looks like a very promising option eventually once it proves itself and gets out of beta but for now I need to stick with what truly – scripter78 May 29 '12 at 14:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.