2

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

ViewContext 被击中时为空。我是否需要在某个地方实例化 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
        }
4

1 回答 1

0

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

于 2016-04-05T13:48:08.480 回答