title: ASP.NET MVC 5 routing optional parameters
body: <p>I've an Action in my ApiController that I want to invoke from a specific link, so I created this simple route</p>
  
  <pre><code>[Route("Rest/GetName/{name}")]
  public IHttpActionResult GetName(string name) {
      // cut - code here is trivial but long, I just fill in an object to return as Json code
      return Json(myObject);
  }
  </code></pre>
  
  <p>It works fine but I want to make the parameter optional. According to <a href="http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx" rel="noreferrer">documentation</a> adding a question point at the end of the parameter name in the route should be enough </p>
  
  <pre><code>[Route("Rest/GetName/{name?}")]
  </code></pre>
  
  <p>This way I get an error if I don't provide the optional parameter, so</p>
  
  <pre><code>.../Rest/GetName/AnyName --&gt; ok
  .../Rest/GetName/ --&gt; error (see below)
  </code></pre>
  
  <blockquote>
    <p>{"Message":"No HTTP resource was found that matches the request URI '<a href="https://localhost/miApp/Rest/GetName" rel="noreferrer">https://localhost/miApp/Rest/GetName</a>'.","MessageDetail":"No action was found on the controller 'Rest' that matches the request."}</p>
  </blockquote>