title: ViewContext.RouteData.Values["Controller"] always Null - Asp.Net Core 2.1
body: <p>I started with the standard ASP.NET Core Web Application template using Asp.Net Core 2.1 as a starting point. </p>
  
  <p>Then i added a left Navigation Panel in the shared folder like this: <a href="https://i.stack.imgur.com/DN83C.png" rel="nofollow noreferrer">ActiveRouteTagHelper</a></p>
  
  <p>In this Navigation Panel I want to highlight the active link and i used the following solution: <a href="https://benjii.me/2017/01/is-active-route-tag-helper-asp-net-mvc-core/" rel="nofollow noreferrer">Ben Cull's ActiveRouteTagHelper for MVC</a> . </p>
  
  <p>And then i encountered the following issue: </p>
  
  <p>When i hit debug and the application starts i expect to see the a controller value in <code>ViewContext.RouteData.Values["Controller"]</code>. </p>
  
  <p>For the first and every other click even though one can see that the <a href="https://i.stack.imgur.com/p5Eit.png" rel="nofollow noreferrer">Url</a> displays the the correct action and controller name</p>
  
  <pre><code>    private bool ShouldBeActive()
      {
          try
          {
              var currentController = ViewContext.RouteData.Values["Controller"].ToString();
              var currentAction = ViewContext.RouteData.Values["Action"].ToString();
  
  
          }
          catch (Exception ex)
          {
              return false;
          }
      }
  </code></pre>
  
  <p>I have removed the rest of the method for clarity. </p>
  
  <p>My <code>Startup</code> Class:</p>
  
  <pre><code>        public void ConfigureServices(IServiceCollection services)
      {
          services.Configure&lt;AppSettings&gt;(_configuration);
          services.Configure&lt;AppSettings&gt;(_configuration.GetSection("PortalSettings"));
          var settings = _configuration.Get&lt;AppSettings&gt;();
  
          services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
          services.AddMvc();
          services.AddMvc().AddControllersAsServices();
          services.TryAddSingleton&lt;IHttpContextAccessor, HttpContextAccessor&gt;();
      }
  
      public void Configure(IApplicationBuilder app, IHostingEnvironment env)
      {
          if (env.IsDevelopment())
          {
              // If this is the case then enable more detailed error output
              app.UseDeveloperExceptionPage();
  
              // Display a more specific error page when an exception occurs connecting to the database
              app.UseDatabaseErrorPage();
          }
          else
          {
              // If this is not the case then forward the error to our generic view
              app.UseExceptionHandler("/Shared/Error");
              app.UseHsts();
          }
  
          app.UseHttpsRedirection();
          app.UseStaticFiles();
  
          app.UseMvc(routes =&gt;
          {
              routes.MapRoute("default", "{controller}/{action}/{id?}");
          });
          app.UseAuthentication();
      }
  </code></pre>
  
  <p>My question is this, how do I get it that these two lines return a value? As no matter what I have tried has proved successful. The outcome is always null!! </p>
  
  <pre><code>var currentController = ViewContext.RouteData.Values["Controller"].ToString();
  var currentAction = ViewContext.RouteData.Values["Action"].ToString();
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/vwhXg.png" rel="nofollow noreferrer">Debug view</a></p>
  
  <p>I have tried various routing mapping options, yet to no avail. Any ideas?</p>