admin管理员组文章数量:1026989
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?
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")
. If, however, I am on a Details page then the id gets included in the url. 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"
. Is this the intended functionality?
Routing map is default.
Javascript as requested:
var orderableDetailUrl = '@Url.Action("Details", "Orderable")';
var resultableDetailUrl = '@Url.Action("Details", "Resultable")';
alert(orderableDetailUrl);
alert(resultableDetailUrl);
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?
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")
. If, however, I am on a Details page then the id gets included in the url. 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"
. Is this the intended functionality?
Routing map is default.
Javascript as requested:
var orderableDetailUrl = '@Url.Action("Details", "Orderable")';
var resultableDetailUrl = '@Url.Action("Details", "Resultable")';
alert(orderableDetailUrl);
alert(resultableDetailUrl);
Share
Improve this question
edited Jul 12, 2013 at 13:36
Quickhorn
asked Jul 11, 2013 at 22:04
QuickhornQuickhorn
1,18112 silver badges23 bronze badges
3
- it would help if you show your javascript and describe what you want to achieve – Dmitry Efimenko Commented Jul 11, 2013 at 22:07
- 1 I never had encountered this behavior, are you sure you use it as intended? – peter Commented Jul 11, 2013 at 22:21
- 1 the UrlHelper methods use the context information, like the current page route data, to create the urls. So, yes, that's intended behavior. As Dimitry says, so us your code. – JotaBe Commented Jul 11, 2013 at 23:10
1 Answer
Reset to default 6As mentioned by @JotaBe in the ments above, the UrlHelper uses the context information. Thus an @Url.Action called for the page that you're currently on will also include optional id/parameters.
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.
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?
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")
. If, however, I am on a Details page then the id gets included in the url. 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"
. Is this the intended functionality?
Routing map is default.
Javascript as requested:
var orderableDetailUrl = '@Url.Action("Details", "Orderable")';
var resultableDetailUrl = '@Url.Action("Details", "Resultable")';
alert(orderableDetailUrl);
alert(resultableDetailUrl);
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?
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")
. If, however, I am on a Details page then the id gets included in the url. 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"
. Is this the intended functionality?
Routing map is default.
Javascript as requested:
var orderableDetailUrl = '@Url.Action("Details", "Orderable")';
var resultableDetailUrl = '@Url.Action("Details", "Resultable")';
alert(orderableDetailUrl);
alert(resultableDetailUrl);
Share
Improve this question
edited Jul 12, 2013 at 13:36
Quickhorn
asked Jul 11, 2013 at 22:04
QuickhornQuickhorn
1,18112 silver badges23 bronze badges
3
- it would help if you show your javascript and describe what you want to achieve – Dmitry Efimenko Commented Jul 11, 2013 at 22:07
- 1 I never had encountered this behavior, are you sure you use it as intended? – peter Commented Jul 11, 2013 at 22:21
- 1 the UrlHelper methods use the context information, like the current page route data, to create the urls. So, yes, that's intended behavior. As Dimitry says, so us your code. – JotaBe Commented Jul 11, 2013 at 23:10
1 Answer
Reset to default 6As mentioned by @JotaBe in the ments above, the UrlHelper uses the context information. Thus an @Url.Action called for the page that you're currently on will also include optional id/parameters.
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.
本文标签: netRazor UrlAction returns with the optional id parameter to a javascript variableStack Overflow
版权声明:本文标题:.net - Razor @Url.Action returns with the optional id parameter to a javascript variable - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745616443a2159319.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论