ViewContext.RouteData.Values["Controller"] always Null - Asp.Net Core 2.1

Viewed 2648

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();

Debug view

I have tried various routing mapping options, yet to no avail. Any ideas?

2 Answers

You are confusing Razor Pages and MVC. The default Web Application template for ASP.NET Core gives you a Razor Pages application (https://www.learnrazorpages.com), and according to the images you posted, that's what you have got.

Routing in Razor Pages is defined according to the file paths of the contents in the Pages folder, not controllers and action methods. If you want an MVC application, choose ASP.NET Core Web Application as the project type, then choose Web Application (Model-View-Controller).

I don't know if this is the right way to do it or not, but I found this blog post that shows how to make that tag helper work with Razor Pages.

Related