title: Add parameter to all Incoming/Outgoing URL's
body: <p>I'm having trouble adding a URL parameter to every URL generated, or redirected to in an ASP MVC 4 application.</p>
  
  <p>I want to generate an ID, and use this ID at any point throughout the application. Storing the id in session is not an option as a single session may have multiple browser windows/tabs open concurrently (each with a different id)</p>
  
  <hr>
  
  <p><strong>RouteConfig</strong></p>
  
  <pre><code>routes.MapRoute(
              name: "Default",
              url: "{controller}/{action}/{customId}",
              defaults: new { controller = "Home", action = "Index", customid = UrlParameter.Optional }
          );
  </code></pre>
  
  <hr>
  
  <p><strong>HomeController.cs</strong></p>
  
  <pre><code>public class HomeController : Controller
  {
      public ActionResult Index()
      {
          var customId = Guid.NewGuid();
  
          ControllerContext.RequestContext.RouteData.Values.Add("customId", customId);
  
          //How do I get this redirect to add customid to the url?
          //E.g. /Home/Start/{customId}
          return RedirectToAction("Start");
          //I could do this: But I want it this to happen for every URL, 
          //and I don't want to replicate this code everywhere
          //return RedirectToAction("Start", new { customId = customId });
      }
  
      public ActionResult Start()
      {
          object customId;
  
          //Redirect Loop
          if (!Request.RequestContext.RouteData.Values.TryGetValue("customId", out customId))
          {
              //To generate the ID
              return RedirectToAction("Index");
          }
  
          ViewData["customId"] = Guid.Parse(customId.ToString());
  
          return View();
      }
  
      public ActionResult Next()
      {
          object customId;
  
          //Redirect Loop
          if (!Request.RequestContext.RouteData.Values.TryGetValue("customId", out customId))
          {
              //To generate the ID
              return RedirectToAction("Index");
          }
  
          ViewData["customId"] = Guid.Parse(customId.ToString());
  
          return View();
      }
  }
  </code></pre>
  
  <hr>
  
  <p>Not only do I want the ID to be automatically inserted into any Redirect results, but when a View is rendered <code>@Url.Action()</code> and <code>@Html.ActionLink()</code> should also add the ID to the generated URL's.</p>
  
  <p><strong>Start.cshtml</strong></p>
  
  <pre><code>@*Both of these should generate an href="~/Home/Next/{customId}"*@
  @Html.ActionLink("Go to Next", "Next", "Home")
  &lt;a href="@Url.Action("Next", "Home")"&gt;Go to Next&lt;/a&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>How do I automatically add an ID to ALL outgoing routes in ASP MVC?</strong></p>