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.
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.
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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With