luego de actualizar la version de .Net core desde la version 3.1 a la version 6 de mi proyecto estoy teniendo una excepcion al momento de buscar un view, independienteme de que use View()
o que use ViewAsPdf()
, los mismos anteriormente funcionaba en la version de 3.1 de .net core .
la excepcion es :System.InvalidOperationException: Unable to find view '~/templates/CropActivityReport.cshtml'. The following locations were searched: ~/templates/CropActivityReport.cshtml
el codigo en el controlador es este:
[HttpGet]
[Route("htmlpdf")]
[AllowAnonymous]
public async Task<IActionResult> DownloadPDFAsync(int cropId, int fieldId, int growerId)
{
var info = await new ReportService(_context, _mapper, _config).CropActivityReportAsync(cropId, fieldId, growerId);
// string HTMLContent = await RazorTemplateEngine.RenderAsync("templates/CropActivityReport.cshtml", info);
return new ViewAsPdf("~/templates/CropActivityReport.cshtml", info) { FileName="Report.pdf"};
}
el Startup.cs:
namespace FieldTrace.WebAPI
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//Adding CORS to services - doesn't mean we use it. Just have it included incase we are in development environment we can app.use it inside Configure method.
services.AddCors(options =>
{
options.AddPolicy("AllowMyOrigin",
builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().WithExposedHeaders("iHeight", "iWidth") );
});
services.AddOptions<EmailSendConfigure>().Bind(Configuration.GetSection("emailConfig"));
services.AddControllers().AddNewtonsoftJson(options =>
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
);
services.AddAutoMapper(typeof(Startup));
var mappingConfig = new MapperConfiguration(mc =>
{
mc.AddProfile(new AutoMapping());
});
IMapper mapper = mappingConfig.CreateMapper();
services.AddSingleton(mapper);
services.AddControllersWithViews();
// Register the Swagger generator, defining 1 or more Swagger documents
services.AddAuthentication(x =>
{
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = Configuration["Jwt:Issuer"],
ValidAudience = Configuration["Jwt:Issuer"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:Key"]))
};
});
services.AddDbContextPool<Context>(options => options
// replace with your connection string
.UseMySQL(Configuration.GetValue<string>("ConnectionString:DefaultConnection")));
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = " WebAPI", Version = "v1" });
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
//If we are in development mode, then enable the CORS so we can test
app.UseCors("AllowMyOrigin");
}
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
// specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("../swagger/v1/swagger.json", " WebAPI V1");
});
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
Rotativa.AspNetCore.RotativaConfiguration.Setup(env.WebRootPath, "Rotativa");
}
}
}
el cshtml esta en la ruta que especifico en el codigo:
algo extraño es que desde visual studio y IIS no funciona pero corriendo el comando dotnet run
si funciona.
alguien sabe como podria solucionar este error, Gracias de antemano.
System.InvalidOperationException: Unable to find view '~/templates/CropActivityReport.cshtml'. The following locations were searched: ~/templates/CropActivityReport.cshtml
luego de haber actualizado la version de Net desde la 3.1 a la version 6