【发布时间】: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();
我尝试了各种路由映射选项,但都无济于事。有什么想法吗?
【问题讨论】:
标签: c# asp.net-mvc asp.net-core-2.1 razor-pages