I am in the process of generating a URL dynamically in my cshtml page. What is the difference between Url.RouteUrl() & Url.Action()?
我正在我的cshtml页面中动态生成一个URL。Url.RouteUrl()和Url.Action()有什么区别?
Which one should I use to generate the URL & what difference do both have in terms of implementation ?
我应该使用哪一个来生成URL ?它们在实现方面有什么不同?
Thanks in advance.
提前谢谢。
2 个解决方案
#1
45
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.
RouteUrl基于路由名生成url。如果您有多个具有相似参数的路由,那么操作方法可能会选择错误的路由——它是基于路由定义的顺序工作的。这可能发生在您的路由有可选参数时。
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.
如果您希望确保使用某个路由url,您需要调用传递此路由名称的RouteUrl。路由名称是唯一的,并且清楚地标识一个路由。
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).
另一个区别是,Action是特定于MVC的(它使用控制器和操作名),而RouteUrl是通用的,并且可以在没有MVC的情况下使用(可以在WebForms中使用路由)。
#2
16
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.
Url。RouteUrl允许您按名称指定特定的路由。这将迫使使用那条路线。Url。操作将只选择符合条件的第一条路径。
#1
45
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.
RouteUrl基于路由名生成url。如果您有多个具有相似参数的路由,那么操作方法可能会选择错误的路由——它是基于路由定义的顺序工作的。这可能发生在您的路由有可选参数时。
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.
如果您希望确保使用某个路由url,您需要调用传递此路由名称的RouteUrl。路由名称是唯一的,并且清楚地标识一个路由。
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).
另一个区别是,Action是特定于MVC的(它使用控制器和操作名),而RouteUrl是通用的,并且可以在没有MVC的情况下使用(可以在WebForms中使用路由)。
#2
16
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.
Url。RouteUrl允许您按名称指定特定的路由。这将迫使使用那条路线。Url。操作将只选择符合条件的第一条路径。