How to setup Rotativa.aspnetcore Configuration in ASP.NET Core 6 MVC

3.2k Views Asked by At

How to add Rotativa.aspnetcore configuration in Program.cs instead of RotativaConfiguration.Setup(env); that was in Startup.cs in .NET 5 and below?

What I have tried this in Program.cs:

var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
RotativaConfiguration.Setup(env);

Thanks

1

There are 1 best solutions below

1
Xinran Shen On BEST ANSWER

In .Net 5 or below version, the configure method like this:

public void Configure(IApplicationBuilder app, 
                      IWebHostEnvironment env,
                      IHostApplicationLifetime lifetime,
                      IService service)
{
    //........
}

In .NET 6, there are a few common services available as top level properties on WebApplication and additional services need to be manually resolved from the IServiceProvider via WebApplication.Services.

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddSingleton<IService, Service>();

var app = builder.Build();

IService service = app.Services.GetRequiredService<IService>();
ILogger logger = app.Logger;
IHostApplicationLifetime lifetime = app.Lifetime;
IWebHostEnvironment env = app.Environment;

//.....

So in your project, You can just configure like this:

IWebHostEnvironment env = app.Environment;
RotativaConfiguration.Setup((Microsoft.AspNetCore.Hosting.IHostingEnvironment)env);

For more information on the difference between the configuration files in .Net 6 and other version, please refer to this link