title: ASP.NET MVC routing with one mandatory parameter and one optional parameter?
body: <p>I've been working on a large MVC application over the past month or so, but this is the first time I've ever needed to define a custom route handler, and I'm running into some problems. Basically I have two parameters to pass. The first one is required and the second one is optional.</p>
  
  <p>I'm following this answer <a href="https://stackoverflow.com/a/2246508">here</a>.</p>
  
  <p>Here is my custom route:</p>
  
  <pre><code>routes.MapRoute(
      "MyRoute",
      "{controller}/{action}/{param1}/{param2}",
      new { 
          controller = "MyController", 
          action = "MyAction", 
          param1 = "", 
          param2 = "" // I have also tried "UrlParameter.Optional" here.
      }
  );
  </code></pre>
  
  <p>And my action method signature:</p>
  
  <pre><code>public ActionResult MyAction(string param1, string param2)
  </code></pre>
  
  <p>If I try the URL <code>http://[myserver]/MyController/MyAction/Test1/Test2</code> then it works like I expect it to, with param1 = "Test1" and param2 = "Test2"</p>
  
  <p>If I try the URL <code>http://[myserver]/MyController/MyAction/Test1</code> then both parameters are null.</p>
  
  <p>Hopefully somebody can tell me what I'm doing wrong here, because I'm lost.</p>