Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 771
Biki Avatar asked Feb 20 '12 08:02

Biki


People also ask

What is URL RouteUrl?

RouteUrl(String, Object) Generates a fully qualified URL for the specified route values by using a route name. RouteUrl(String, RouteValueDictionary) Generates a fully qualified URL for the specified route values by using a route name.

Is Route same as URL?

A "route" is typically code that matches incoming request paths to resources. In other words, it defines the URL and what code will be executed.

What is URL pattern in MVC?

URL patterns for routes in MVC Applications typically include {controller} and {action} placeholders. When a request is received, it is routed to the UrlRoutingModule object and then to the MvcHandler HTTP handler.


1 Answers

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).

like image 136
Jakub Konecki Avatar answered Sep 21 '22 00:09

Jakub Konecki