简体   繁体   中英

Accessing Route Data from Tag Helper in ASP.NET 5 RC1

I have been using the solution below with beta 5, but this no longer works in RC1 : How to access RouteData from an ASP.Net 5 Tag Helper in MVC 6

ViewContext is null when it gets hit. Do I need to instantiate the ViewContext somewhere eg in Startup?

EDIT : Here is my ConfigureServices method :

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
        }

Did you use the correct ViewContext attribute? ViewContext attribute is in Microsoft.AspNet.Mvc.ViewFeatures, not in Microsoft.AspNet.Mvc.Rendering (where ViewContext class itself is).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM