There's no built-in way to achieve this. About the most reusable way I can think of is to create a UrlHelper
extension, and then use that render all URLs across your site:
public static class UrlHelperExtensions
{
public static string MyAction(this UrlHelper urlHelper, string actionName)
{
return urlHelper.Action(actionName) + "?extra=code";
}
}
Basically proxying all the parameters from your version of the method to the original version, while adding your extra stuff to the end of the generated URL. However, for completeness, you would need to provide equivalent extensions for each of the method signatures (see: https://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper.action(v=vs.118).aspx#M:System.Web.Mvc.UrlHelper.Action), as well as all the signatures for Url.RouteUrl
, in case that is used instead. Additionally, if you want to take advantage of the HTML helpers like Html.ActionLink
, you'll need to do the same thing for HtmlHelper
as well, creating equivalent extensions for each of the signatures for that as well as Html.RouteLink
. Depending on how deep down the rabbit hole you want to go, you may also need to do the same for things like Html.BeginForm
. Even with all that, the center only holds as long as every developer for all time that touches the app always uses the custom extensions rather than the built-in helpers for every link.
As should be apparent, this is non-trivial. I would second-guess any business case that required this. Can it be solved another way, a simpler way? For example, if you just need to persist this code between requests, maybe you should just add it to the session? Then, the format of the URL no longer matters.