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.