Difference between Url.RouteUrl() & Url.Action() in MVC3

Biki picture Biki · Feb 20, 2012 · Viewed 15.1k times · Source

I am in the process of generating a URL dynamically in my cshtml page. What is the difference between Url.RouteUrl() & Url.Action()?

Which one should I use to generate the URL & what difference do both have in terms of implementation ?

Thanks in advance.

Answer

Jakub Konecki picture Jakub Konecki · Feb 20, 2012

RouteUrl generated the url based on route name. If you have multiple routes with similar parameters the Action method may pick a wrong one - it works based on the order of route definitions. This may take place when your routes have optional parameters.

If you want to make sure that a certain route url will be used you need to call RouteUrl passing this route name. Route names are unique and clearly identifies a route.

One more difference is that Action is MVC specific (it uses controller and action names), while RouteUrl is generic is and can be used without MVC (you can have routing in WebForms).