When Urls are autogenerated using the Url.Action helper, if a page contains a line similar to
@Url.Action(“Edit”,”Student”)
is expected to generate a url like domain/student/edit and its working as expected.
But if the requested url contains some parameters, like domain/student/edit/210, the above code uses these parameters from the previous request and generates something similar even though I’ve not provided any such parameter to the Action method.
In short, if the requested url contains any parameters, any auto generated links of the page (served for that request) will include those parameters as well no matter if I specify them or not in the Url.Action method.
What’s going wrong?
Weird, can’t seem to reproduce the problem:
and inside
Index.cshtml:Now when I request
/home/index/123the url helper generates/home/aboutas expected. No ghost parameters. So how does your scenario differs?UPDATE:
Now that you have clarified your scenario it seems that you have the following:
and inside
Index.cshtmlyou are trying to use:If you request
/home/index/123this generates/home/index/123instead of the expected/home/index(or simply/taken into account default values).This behavior is by design. If you want to change it you will have to write your own helper which ignores the current route data. Here’s how it might look:
This will generate the proper url you were expecting. Of course this is ugly. I would recommend you encapsulating it into a reusable helper.