title: ASP.NET Core Razor Pages Routing With parameters
body: <p>I have a simple page where users can search for records based on start and end date, and the Reference and Client fields are optional</p>
  
  <p>on the details page I have this</p>
  
  <pre><code>@page "{datestart}/{dateend}/{referenceId?}/{client?}"
  </code></pre>
  
  <p>on the search page I have this post handler</p>
  
  <pre><code>    public IActionResult OnPost()
      {
          if (!ModelState.IsValid)
          {
              var options = new MemoryCacheEntryOptions() { AbsoluteExpiration = DateTime.Now.AddMinutes(10) };
              ViewData["ReferenceId"] = new SelectList(_context.Referencias.AsNoTracking().FromCache(), "Id", "Name");
  
              return Page();
          }
  
          return RedirectToPage("Details", new
          {
              datestart= SearchViewModel.DateStart.ToString("dd-MM-yyyy"),
              dateend = SearchViewModel.DateEnd.ToString("dd-MM-yyyy"),
              referenceId = SearchViewModel.ReferenceId,
              client = SearchViewModel.Client
          });
      }
  </code></pre>
  
  <p>However everything works well except when the Reference field is null</p>
  
  <p>on my details page</p>
  
  <pre><code>  public void OnGet(string datestart, string dateend, int? referenceId, int? client)
  </code></pre>
  
  <p>The intended result was that i would be able to go to details page if i don't supply a referenceId event if i did supply a client.</p>
  
  <p>The dates are always required though.</p>
  
  <p>So is there a way that i can still route even if referenceId is not supplied but client is?</p>
  
  <p>All i get is this exception</p>
  
  <blockquote>
    <p>InvalidOperationException: No page named 'Details' matches the
    supplied values.
    Microsoft.AspNetCore.Mvc.Infrastructure.RedirectToPageResultExecutor.ExecuteAsync(ActionContext
    context, RedirectToPageResult result)
    Microsoft.AspNetCore.Mvc.RedirectToPageResult.ExecuteResultAsync(ActionContext
    context)
    Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultAsync(IActionResult
    result)
    Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResultFilterAsync()
    Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResultExecutedContext
    context)
    Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.ResultNext(ref State next, ref Scope scope, ref object state, ref
    bool isCompleted)
    Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultFilters()
    Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
    Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext
    context) Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref
    State next, ref Scope scope, ref object state, ref bool isCompleted)
    Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
    Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
    Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext
    httpContext)
    Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext
    httpContext)
    Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext
    context)
    Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext
    context)
    NToastNotify.NtoastNotifyAjaxToastsMiddleware.InvokeAsync(HttpContext
    context, RequestDelegate next)
    Microsoft.AspNetCore.Builder.UseMiddlewareExtensions+&lt;>c__DisplayClass5_1+&lt;b__1>d.MoveNext()
    Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext
    context)
    Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext
    context)</p>
  </blockquote>
  
  <p>Also I tried switch the order of {referenceId?} and {client?} and it worked but then it fails if i do the same for referenceId.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>As per @Nick suggestion I tried once again with multiple routes and it except for the 2nd route, it won't route if it's the only one.</p>
  
  <pre><code>options.Conventions.AddAreaPageRoute("Production","/BackOffice/Account/Records/Details", "{datestart}/{dateend}/{client?}");
               options.Conventions.AddAreaPageRoute("Production", "/BackOffice/Account/Records/Details", "{datestart}/{dateend}/{referenceId?}");
               options.Conventions.AddAreaPageRoute("Production", "/BackOffice/Account/Records/Details", "{datestart}/{dateend}/{client?}/{referenceId?}");
  </code></pre>