【问题标题】:ViewContext.RouteData.Values["Controller"] always Null - Asp.Net Core 2.1ViewContext.RouteData.Values["Controller"] 始终为 Null - Asp.Net Core 2.1
【发布时间】:2019-04-15 05:57:09
【问题描述】:

我从使用 Asp.Net Core 2.1 作为起点的标准 ASP.NET Core Web 应用程序模板开始。

然后我在共享文件夹中添加了一个左侧导航面板,如下所示:ActiveRouteTagHelper

在此导航面板中,我想突出显示活动链接,我使用了以下解决方案:Ben Cull's ActiveRouteTagHelper for MVC。

然后我遇到了以下问题:

当我点击调试并启动应用程序时,我希望看到ViewContext.RouteData.Values["Controller"] 中的控制器值。

即使可以看到Url 显示正确的操作和控制器名称,但对于第一次和所有其他点击

    private bool ShouldBeActive()
    {
        try
        {
            var currentController = ViewContext.RouteData.Values["Controller"].ToString();
            var currentAction = ViewContext.RouteData.Values["Action"].ToString();


        }
        catch (Exception ex)
        {
            return false;
        }
    }

为了清楚起见,我已经删除了该方法的其余部分。

我的Startup班级:

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

我的问题是,我如何知道这两行返回一个值?因为无论我尝试过什么都证明是成功的。结果始终为空!!

var currentController = ViewContext.RouteData.Values["Controller"].ToString();
var currentAction = ViewContext.RouteData.Values["Action"].ToString();

Debug view

我尝试了各种路由映射选项,但都无济于事。有什么想法吗?

【问题讨论】:

    标签: c# asp.net-mvc asp.net-core-2.1 razor-pages


    【解决方案1】:

    您混淆了 Razor Pages 和 MVC。 ASP.NET Core 的默认 Web 应用程序模板为您提供了 Razor Pages 应用程序 (https://www.learnrazorpages.com),根据您发布的图像,这就是您所拥有的。

    Razor Pages 中的路由是根据 Pages 文件夹中内容的文件路径定义的,而不是控制器和操作方法。如果您想要 MVC 应用程序,请选择 ASP.NET Core Web Application 作为项目类型,然后选择 Web Application (Model-View-Controller)。

    【讨论】:

    • 感谢 Mike,你说得对,我正在尝试学习 ASP.NET 核心剃须刀页面而不是 MVC。我仍然想突出菜单项,我会做更多的阅读,只是在这一点上都非常混乱
    【解决方案2】:

    我不知道这样做是否正确,但我发现 this blog post 显示了如何使该标签助手与 Razor Pages 一起使用。

    【讨论】:

    • 抱歉,您能否详细说明您认为此链接回答问题的原因?我根本不认为它们是同一个问题。也许我错过了什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-07
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    相关资源
    最近更新 更多