ViewContext.RouteData.Values["Controller"] 始终为 Null - Asp.Net Core 2.1
ViewContext.RouteData.Values["Controller"] always Null - Asp.Net Core 2.1
我从标准 ASP.NET 核心 Web 应用程序模板开始,使用 Asp.Net 核心 2.1 作为起点。
然后我在共享文件夹中添加了一个左侧导航面板,如下所示:ActiveRouteTagHelper
在此导航面板中,我想突出显示活动的 link 并且我使用了以下解决方案: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
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();
}
我的问题是,我如何得到这两行return一个值?因为无论我尝试过什么,都证明是成功的。结果总是空的!!
var currentController = ViewContext.RouteData.Values["Controller"].ToString();
var currentAction = ViewContext.RouteData.Values["Action"].ToString();
Debug view
我尝试了各种路由映射选项,但都无济于事。有什么想法吗?
您混淆了 Razor Pages 和 MVC。 ASP.NET Core 的默认 Web 应用程序模板为您提供了一个 Razor Pages 应用程序 (https://www.learnrazorpages.com),根据您发布的图像,这就是您所拥有的。
Razor Pages 中的路由是根据页面文件夹中内容的文件路径定义的,而不是控制器和操作方法。如果您想要 MVC 应用程序,请选择 ASP.NET Core Web Application 作为项目类型,然后选择 Web Application (Model-View-Controller).
我不知道这样做是否正确,但我发现 this blog post 展示了如何使该标签助手与 Razor Pages 一起工作。
我从标准 ASP.NET 核心 Web 应用程序模板开始,使用 Asp.Net 核心 2.1 作为起点。
然后我在共享文件夹中添加了一个左侧导航面板,如下所示:ActiveRouteTagHelper
在此导航面板中,我想突出显示活动的 link 并且我使用了以下解决方案: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
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();
}
我的问题是,我如何得到这两行return一个值?因为无论我尝试过什么,都证明是成功的。结果总是空的!!
var currentController = ViewContext.RouteData.Values["Controller"].ToString();
var currentAction = ViewContext.RouteData.Values["Action"].ToString();
Debug view
我尝试了各种路由映射选项,但都无济于事。有什么想法吗?
您混淆了 Razor Pages 和 MVC。 ASP.NET Core 的默认 Web 应用程序模板为您提供了一个 Razor Pages 应用程序 (https://www.learnrazorpages.com),根据您发布的图像,这就是您所拥有的。
Razor Pages 中的路由是根据页面文件夹中内容的文件路径定义的,而不是控制器和操作方法。如果您想要 MVC 应用程序,请选择 ASP.NET Core Web Application 作为项目类型,然后选择 Web Application (Model-View-Controller).
我不知道这样做是否正确,但我发现 this blog post 展示了如何使该标签助手与 Razor Pages 一起工作。