I started with the standard ASP.NET Core Web Application template using Asp.Net Core 2.1 as a starting point.
Then i added a left Navigation Panel in the shared folder like this: ActiveRouteTagHelper
In this Navigation Panel I want to highlight the active link and i used the following solution: Ben Cull's ActiveRouteTagHelper for MVC .
And then i encountered the following issue:
When i hit debug and the application starts i expect to see the a controller value in ViewContext.RouteData.Values["Controller"].
For the first and every other click even though one can see that the Url displays the the correct action and controller name
private bool ShouldBeActive()
{
try
{
var currentController = ViewContext.RouteData.Values["Controller"].ToString();
var currentAction = ViewContext.RouteData.Values["Action"].ToString();
}
catch (Exception ex)
{
return false;
}
}
I have removed the rest of the method for clarity.
My Startup Class:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<AppSettings>(_configuration);
services.Configure<AppSettings>(_configuration.GetSection("PortalSettings"));
var settings = _configuration.Get<AppSettings>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddMvc();
services.AddMvc().AddControllersAsServices();
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}
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 =>
{
routes.MapRoute("default", "{controller}/{action}/{id?}");
});
app.UseAuthentication();
}
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!!
var currentController = ViewContext.RouteData.Values["Controller"].ToString();
var currentAction = ViewContext.RouteData.Values["Action"].ToString();
I have tried various routing mapping options, yet to no avail. Any ideas?