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

元气小坏坏 提交于 2019-12-03 14:38:49

问题


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.


回答1:


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




回答2:


Url.RouteUrl allows you to specify a particular route by name. This will force the usage of that route. Url.Action will simply pick the first route that matches the criteria.



来源:https://stackoverflow.com/questions/9357873/difference-between-url-routeurl-url-action-in-mvc3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!