gpt4 book ai didi

c# - 在 ASP.NET 5 RC1 中从 Tag Helper 访问路由数据

转载 作者:太空宇宙 更新时间:2023-11-03 15:29:24 26 4
gpt4 key购买 nike

我一直在 beta 5 中使用下面的解决方案,但这在 RC1 中不再有效: How to access RouteData from an ASP.Net 5 Tag Helper in MVC 6

ViewContext 被击中时为 null。我是否需要在某处实例化 ViewContext,例如在 Startup 中?

编辑:这是我的 ConfigureServices 方法:

public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);

//EF 7 setup
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));

services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();

// Add MVC services to the services container.
services.AddMvc();

//Add Cors support to the service
services.AddCors();

var policy = new Microsoft.AspNet.Cors.Infrastructure.CorsPolicy();

policy.Headers.Add("*");
policy.Methods.Add("*");
policy.Origins.Add("*");
policy.SupportsCredentials = true;

services.Configure<CorsOptions>(x => x.AddPolicy("mypolicy", policy));

// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
services.AddScoped<IMainRepository, MainRepository>(); //dependency injection config
}

最佳答案

您是否使用了正确的 ViewContext 属性? ViewContext 属性在 Microsoft.AspNet.Mvc.ViewFeatures 中,而不在 Microsoft.AspNet.Mvc.Rendering 中(ViewContext 类本身所在的位置)。

关于c# - 在 ASP.NET 5 RC1 中从 Tag Helper 访问路由数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34238336/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com