0

When I type the URL like ../Search/Recipes/Deserts/Pie, I can get redirected to that page (i.e in SearchController, action method would be Recipes & parameters would be Deserts & Pie .

NB: To achieve the same I ve added a route in my Global.asax, that looks like :

    routes.MapRoute(
            "SearchRecipes",                                              
            "Search/Recipes/{category}/{type}",                           
            new { controller = "Search", action = "Recipes", category = "all" , type = ""}  
 );

But how to generate that URL through our code? When I try to add a route value in Url.Action, it comes as a query string.

For example,

Url.Action( "Recipes" , "Search" , new { type = item.type, isPie = item.isPie} )

would give me link that looks like : Search/Recipes?type=Deserts&isPie=Y,

but I want to generate url like Search/Recipes/Deserts/Y.

Please let me know how we can achieve the same?

Basically I dont want to pass my parameters as query string, but want to adhere to \ separated values.

3
  • This new route looks exactly the same as the last route, except the parameters are different. How is the routing engine supposed to tell them apart? Commented Feb 13, 2012 at 12:44
  • Or do you mean why isn't this action getting matched to the existing route? Commented Feb 13, 2012 at 12:45
  • @32bitkid : Basically I dont want to pass my parameters as query string, but want to adhere to \ separated values. Commented Feb 13, 2012 at 12:52

2 Answers 2

1

Try

 routes.MapRoute(
        "SearchRecipes",                                              
        "Search/Recipes/{category}/{type}/{isPie}",                           
        new { controller = "Search", action = "Recipes", category = "all" , type = "", isPie="N"}  
  );
@Url.RouteUrl("SearchRecipes",new{category=item.Category,type=item.type,isPie=itme.isPie})

Any query parameter not specified when defining a route will be added as ?param=value

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

5 Comments

I am getting "/" as the value in my url, i.e. string url = @Url.RouteUrl("SearchRecipes",new{category=item.Category,type=item.type,isPie=itme.isPie});
Can you post your route definitions?
My route defination is routes.MapRoute("myRoute", "{controller}/{action}/{name}/{age}/{address}/{zip}/{opt}", new { controller = "Home", action = "GetStuff", age = 0, address = "DummyAddress", zip = 0, name = "Dell", opt = UrlParameter.Optional } );
and string url = @Url.RouteUrl("myRoute", new { name = Model.Nmae, age = Model.Age, address = Model.Address, zip = Model.zipCode, opt = Model.OptParameter }); } is yielding "/" as its value.
Afaik, the route is yelding '/' when the route values match the route defaults. Try debugging to see exactly what values goes to the helper
0

You can use the UrlHelper.RouteUrl method

Url.RouteUrl("SearchRecipes", new {category = item.category, type=item.type});

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.