title: Asp.net core mvc optional parameter for Index
body: <p>im currently trying to create a Controller in ASP.net core mvc with an optional parameter &quot;id&quot;.
  Im fairly new to asp.net, I've tried to check other posts but nothing solved my problem.</p>
  <pre><code>public class TestController : Controller
  {
      private readonly ITestRepository _TestRepository;
  
      public TestController(ITestRepository TestRepository)
      {
          _TestRepository = TestRepository;
      }
  
      public async Task&lt;IActionResult&gt; Index(string id)
      {
          if (string.IsNullOrEmpty(id))
          {
              return View(&quot;Search&quot;);
          }
          var lieferscheinInfo = await _TestRepository.GetTestdata(Convert.ToInt64(id));
          if (lieferscheinInfo == null)
          {
              // return  err view
              throw new Exception(&quot;error todo&quot;);
          }
          return View(lieferscheinInfo);
  
      }
  }
  </code></pre>
  <p>I want to open the site like this &quot;localhost:6002/Test&quot; or &quot;localhost:6002/Test/123750349&quot; e.g, the parameter can be an int as well, i've tried both(string and int) but it doesnt work.
  Either the site returns 404 (for both cases, with and without an parameter) or the parameter gets ignored and is always null.
  I've tried to add <code>[Route(&quot;{id?}&quot;)]</code> on the Index but it did not change anything.</p>
  <p>Greetings</p>