[英]Razor @Url.Action returns with the optional id parameter to a javascript variable
Is it intended that the @Url.Action method returns the current url if the user is currently within the controller and action that the parameters reference? 如果用户当前在控制器和参数引用的操作中,是否打算@ Url.Action方法返回当前url?
I have a simple setup for our controllers. 我有一个简单的控制器设置。
OrderableTest/Details/Id
ResultableTest/Details/Id
If I call @Url.Action("Details", "Orderable")
from the home controller (Home/Index)
or from the Resultable/Details
I will get the proper URL saved to a javascript variable ("/Orderable/Details")
. 如果我从家庭控制器(Home/Index)
或Resultable/Details
调用@Url.Action("Details", "Orderable")
,我会将正确的URL保存到javascript变量("/Orderable/Details")
。 If, however, I am on a Details page then the id gets included in the url. 但是,如果我在“详细信息”页面上,则ID将包含在网址中。 For example, I am on the page Orderable/Details/12345
and I call @Url.Action("Details", "Orderable")
, instead of getting "/Orderable/Details"
I get "/Orderable/Details/12345"
. 例如,我在@Url.Action("Details", "Orderable")
Orderable/Details/12345
页面上,我调用@Url.Action("Details", "Orderable")
,而不是获得"/Orderable/Details"
我得到"/Orderable/Details/12345"
。 Is this the intended functionality? 这是预期的功能吗?
Routing map is default. 路由映射是默认的。
Javascript as requested: Javascript按要求:
var orderableDetailUrl = '@Url.Action("Details", "Orderable")';
var resultableDetailUrl = '@Url.Action("Details", "Resultable")';
alert(orderableDetailUrl);
alert(resultableDetailUrl);
As mentioned by @JotaBe in the comments above, the UrlHelper uses the context information. 正如@JotaBe在上面的评论中所提到的,UrlHelper使用上下文信息。 Thus an @Url.Action called for the page that you're currently on will also include optional id/parameters. 因此,调用您当前所在页面的@ Url.Action还将包含可选的id /参数。
To get around this, you can pass in a blank optional parameter to the method. 要解决此问题,您可以将空白的可选参数传递给方法。 For my problem above, it was solved with the following code 对于我上面的问题,它使用以下代码解决
var resultableDetailUrl = '@Url.Action("Details", "Resultable", new { id = "" })';
var orderableDetailUrl = '@Url.Action("Details", "Orderable", new { id = "" })';
Which both will properly return /Orderable/Details/ and /Resultable/Details/ regardless of which page the user is currently on. 两者都将正确返回/可订购/详细信息/和/可结果/详细信息/无论用户当前在哪个页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.