简体   繁体   中英

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

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?

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) .

我不知道这是否是正确的方法,但我发现这篇博客文章展示了如何使该标签助手与 Razor Pages 一起工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM