Asp.net MVC 3 routing issue with UrlHelper.Action?

The way they are defined now, I couldn't get it to work. Since you don't have a {controller} nor {action} placeholder defined for them, it will always user the SearchController and it's method Index; yet you define other actions in you helper function (Search, Searchkeyword and Searchlocation). When I changed the routes accordingly, I had no problem at all @Url.

SearchLink("key1", "",0) return /key1-jobs throughout my entire app..

Up vote 0 down vote favorite share g+ share fb share tw.

I have caught in a issue related to how helper Action method and routing work. I have created a helper method to create search links public static string SearchLink(this System.Web.Mvc. UrlHelper helper, string key, string loc, int p) { key = Tools.

GetLinkRewrite(key.Trim().ToLower()); loc = Tools. GetLinkRewrite(loc.Trim().ToLower()); string url = ""; if (!String. IsNullOrEmpty(key) &&!String.

IsNullOrEmpty(loc)) { if (p > 0) url = helper. Action("Index", "Search", new { keyword = key, location = loc, page = p }); else url = helper. Action("Index", "Search", new { keyword = key, location = loc }); }else if (!String.

IsNullOrEmpty(key)) { if (p > 0) url = helper. Action("Index", "Search", new { keyword = key, page = p }); else url = helper. Action("Index", "Search", new { keyword = key }); }else if (!String.

IsNullOrEmpty(loc)) { if (p > 0) url = helper. Action("Index", "Search", new { location = loc, page = p }); else url = helper. Action("Index", "Search", new { location = loc }); }else url = helper.

Action("Index", "Search"); return url. Replace("%20","+"); } And here is my routing routes. MapRoute( "Search", "{keyword}-jobs-in-{location}", new { controller = "Search", action = "Index" } ); routes.

MapRoute( "Searchlocation", "jobs-in-{location}", new { controller = "Search", action = "Index" } ); routes. MapRoute( "Searchkeyword", "{keyword}-jobs", new { controller = "Search", action = "Index" } ); Now when I call SearchLink from any action as with parameters eq. Url.

SearchLink("asp.net", "", 0), it give me correct url as /asp. Net-jobs But if call same method from Index action of SearchController with same parameters it give me /asp. Net-jobs-in- Although I can achieve correct behavior(thank Craig for giving hint) by using RouteUrl instead of Action in above extension method, But I want to understand why it's happing.

Thanks in advance asp. Net-mvc asp. Net-mvc-3 asp.

Net-mvc-routing link|improve this question edited Sep 17 '11 at 19:36 asked Sep 6 '11 at 19:28Neo11.

Yes, I want to call Index action of SearchController for 3 urls. I have only 2 action in SearchController "Index" and "RecentSearch". On recentsearch view url generated are correct for all combination of keyword and location.

On Index view I am showing some similar search links that are not generated correctly using same helper method. – Neo Sep 15 '11 at 17:47 What other routes did or do you have registered? I seem to have trouble to make this work.

If I just use the routes you have, and I browse to localhost/asp. Net-jobs (the only way to make it work) the url = helper. Action("Searchkeyword", new { keyword = key }); returns null upon calling Url.

Searchlink("myKey","",0) reason for this is that there's no action with the name Searchkeyword in the SearchController. The first parameter of Url. Action is the name of an action, not the name of route.

So I am curious how this works for you.... – Major Byte Sep 15 '11 at 19:20 Oh, I am really sorry. I have updated the extension method in my question. I messed up the function code while making fixes and posting on SO.

If you want I can send you a sample project code showing problem. – Neo Sep 17 '11 at 19:41.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions