title: ASP.NET MVC Route Value ID with Period
body: <p>I have an MVC5 web application where some ID's can contain the period character.</p>
  
  <p>For example</p>
  
  <p>This works fine
  <a href="http://www.website.com/Employee/Details/123" rel="nofollow noreferrer">http://www.website.com/Employee/Details/123</a></p>
  
  <p>This will fail
  <a href="http://www.website.com/Employee/Details/123.45" rel="nofollow noreferrer">http://www.website.com/Employee/Details/123.45</a></p>
  
  <p>But this will work fine
  <a href="http://www.website.com/Employee/Details/?id=123.45" rel="nofollow noreferrer">http://www.website.com/Employee/Details/?id=123.45</a></p>
  
  <p>Similar problem here:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/294495/semantic-urls-with-dots-in-net">Semantic urls with dots in .net</a></li>
  <li><a href="https://stackoverflow.com/questions/332757/asp-net-mvc-how-to-route-search-term-with-period-at-the-end">ASP.NET MVC: How to Route Search Term with . (Period) at the end</a></li>
  </ul>
  
  <p>Currently all the solutions I have found are described in the links above.</p>
  
  <ol>
  <li><p><code>&lt;httpRuntime relaxedUrlToFileSystemMapping="true" /&gt;</code></p></li>
  <li><p><code>&lt;add name="UrlRoutingHandler" type="System.Web.Routing.UrlRoutingHandler, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" path="Remote.mvc/*" verb="GET"/&gt;</code></p></li>
  </ol>
  
  <p>As users will only browse to these url's via menus and link in the web page, I was hoping that the ActionLink html helper could be smart enough to render the ?id=123.45 when it recognizes the Id contains unsafe characters for the the Id route value.</p>
  
  <p>Or is there a way to force the ActionLink html helper to always render the Id route value in the query string? </p>
  
  <p><strong>Update</strong>
  I could not find any parameters to force ActionLink to render the ?id= query string parameter so I found 2 possible solutions.</p>
  
  <p><strong>Solution 1</strong></p>
  
  <p>Create an html helper to generate the url.
  Any comments for improvement on this code would be appreciated.</p>
  
  <pre><code>        public static MvcHtmlString ActionLinkSafeId(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
      {
  
          var routeValuesDictionary = new RouteValueDictionary(routeValues);
          MvcHtmlString link = htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributes);
          if (routeValuesDictionary.ContainsKey("id") &amp;&amp; routeValuesDictionary["id"].ToString().Contains('.'))
          {
              if (routeValuesDictionary.Count == 1)
              {
                  var searchValue = string.Format("/{0}", routeValuesDictionary["id"]);
                  var replaceValue = string.Format("?id={0}", routeValuesDictionary["id"]);
                  link = new MvcHtmlString(link.ToString().Replace(searchValue, replaceValue));
              }
              else
              {
                  var searchValue = string.Format("/{0}?", routeValuesDictionary["id"]);
                  var replaceValue = string.Format("?id={0}&amp;", routeValuesDictionary["id"]);
                  link = new MvcHtmlString(link.ToString().Replace(searchValue, replaceValue));
              }
          }
  
          return link;
      }
  </code></pre>
  
  <p><strong>Solution 2</strong></p>
  
  <p>Disable the use of id route value in your route configuration.
  This can either be done for the default route, a specific controller or a specific controller/action.</p>
  
  <pre><code>            routes.MapRoute(
              name: "Default",
              url: "{controller}/{action}",
              defaults: new { controller = "Home", action = "Index"}
          );
  </code></pre>