I have a problem going from a controller to a view. I have an error which tells me that the view was not found except that it exists and that it is in the right place.
What’s weird is that locally when I add .AddRazorRuntimeCompilation(); then it works.
But if I remove this part or test in production, then it doesn’t work anymore.
here is the tree :
Error:
InvalidOperationException: The view 'Index' was not found. The following locations were searched:
/Areas/BackOffice/Views/Users/Index.cshtml
/Areas/BackOffice/Views/Shared/Index.cshtml
/Views/Shared/Index.cshtml
/Pages/Shared/Index.cshtml
Controler:
[Area("BackOffice")]
[Authorize(Policy = "BackOfficePolicy")]
public class UsersController : Controller
{
private readonly UserManager<IdentityUser> _userManager;
public UsersController(UserManager<IdentityUser> userManager)
{
_userManager = userManager;
}
[Authorize]
public IActionResult Index()
{
var users = _userManager.Users.ToList();
return View(users);
}
}
Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews().AddRazorRuntimeCompilation();
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"))
);
builder.Services.AddIdentity<IdentityUser, IdentityRole>()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();
builder.Services.AddSingleton<IEmailSender, EmailSender>();
builder.Services.AddRazorPages();
builder.Services.AddAutoMapper(typeof(Program));
builder.Services.AddAuthorization(options =>
{
options.AddPolicy("BackOfficePolicy", policy =>
policy.RequireRole("BackOffice"));
});
builder.Services.ConfigureApplicationCookie(options =>
{
options.AccessDeniedPath = new PathString("/Identity/Account/AccessDenied");
});
var app = builder.Build();
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
app.UseDeveloperExceptionPage();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
//app.UseExceptionHandler("/Reporting/Home/Error");
app.UseHsts();
}
var supportedCultures = new[]
{
new CultureInfo("fr-FR")
};
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("fr-BE"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
});
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapRazorPages();
app.MapControllerRoute(
name: "default",
pattern: "{area=Reporting}/{controller=Reports}/{action=Index}/{id?}");
app.Run();
I have the impression that this comes from the fact that I have different areas but yet the view is located where the error tells me that it is sought.
Am I missing something?
(I’m using NET.7)
EDIT:
CSproj file:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>disable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="7.0.11" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="7.0.11" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="7.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.11">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.10" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.11" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..DataAccessDataAccess.csproj" />
<ProjectReference Include="..UtilityUtility.csproj" />
</ItemGroup>
<ItemGroup>
<!-- extends watching group to include *.cshtml and *.razor files -->
<Watch Include="***.cshtml;*.razor;*.js;*.css" Exclude="**obj***;bin***" />
</ItemGroup>
<ItemGroup>
<Content Remove="AreasBackOfficeViewsHomeEVTAccomodationDistanceVL.cshtml" />
<Content Remove="AreasBackOfficeViewsHomeEVTAccomodationHotelVL.cshtml" />
<Content Remove="AreasBackOfficeViewsHomeEVTCountryVL.cshtml" />
<Content Remove="AreasBackOfficeViewsHomeEVTFoodCategoryVL.cshtml" />
<Content Remove="AreasBackOfficeViewsHomeEVTFoodDishesVL.cshtml" />
<Content Remove="AreasBackOfficeViewsHomeEVTFoodSeasonVL.cshtml" />
<Content Remove="AreasBackOfficeViewsHomeEVTFoodTypeVL.cshtml" />
<Content Remove="AreasBackOfficeViewsHomeEVTFreightVL.cshtml" />
<Content Remove="AreasBackOfficeViewsHomeEVTSeasonVL.cshtml" />
<Content Remove="AreasBackOfficeViewsHomeEVTtechnicalProductionVL.cshtml" />
<Content Remove="AreasBackOfficeViewsHomeEVTTypeOfBuildingVL.cshtml" />
<Content Remove="AreasBackOfficeViewsHomeEVTTypeOfHeatingVL.cshtml" />
<Content Remove="AreasBackOfficeViewsHomeParticipantOriginVL.cshtml" />
<Content Remove="AreasBackOfficeViewsHomeTransportVL.cshtml" />
<Content Remove="AreasBackOfficeViewsUsersIndex.cshtml" />
<Content Remove="AreasReportingViewsReportsEvent.cshtml" />
<Content Remove="AreasReportingViewsReportsEVTVenuesEVT.cshtml" />
<Content Remove="ViewsShared_LayoutReporting.cshtml" />
</ItemGroup>
<ItemGroup>
<Watch Remove="AreasBackOfficeViewsHomeEVTAccomodationDistanceVL.cshtml" />
<Watch Remove="AreasBackOfficeViewsHomeEVTAccomodationHotelVL.cshtml" />
<Watch Remove="AreasBackOfficeViewsHomeEVTCountryVL.cshtml" />
<Watch Remove="AreasBackOfficeViewsHomeEVTFoodCategoryVL.cshtml" />
<Watch Remove="AreasBackOfficeViewsHomeEVTFoodDishesVL.cshtml" />
<Watch Remove="AreasBackOfficeViewsHomeEVTFoodSeasonVL.cshtml" />
<Watch Remove="AreasBackOfficeViewsHomeEVTFoodTypeVL.cshtml" />
<Watch Remove="AreasBackOfficeViewsHomeEVTFreightVL.cshtml" />
<Watch Remove="AreasBackOfficeViewsHomeEVTSeasonVL.cshtml" />
<Watch Remove="AreasBackOfficeViewsHomeEVTtechnicalProductionVL.cshtml" />
<Watch Remove="AreasBackOfficeViewsHomeEVTTypeOfBuildingVL.cshtml" />
<Watch Remove="AreasBackOfficeViewsHomeEVTTypeOfHeatingVL.cshtml" />
<Watch Remove="AreasBackOfficeViewsHomeParticipantOriginVL.cshtml" />
<Watch Remove="AreasBackOfficeViewsHomeTransportVL.cshtml" />
<Watch Remove="AreasBackOfficeViewsUsersIndex.cshtml" />
<Watch Remove="AreasReportingViewsReportsEvent.cshtml" />
<Watch Remove="AreasReportingViewsReportsEVTVenuesEVT.cshtml" />
<Watch Remove="AreasReportingViewsReportsEVT_LiveEVTCO2Result.cshtml" />
<Watch Remove="ViewsShared_LayoutReporting.cshtml" />
</ItemGroup>
<ItemGroup>
<None Include="AreasBackOfficeViewsHomeEVTAccomodationDistanceVL.cshtml" />
<None Include="AreasBackOfficeViewsHomeEVTFoodCategoryVL.cshtml" />
<None Include="AreasBackOfficeViewsHomeEVTFoodDishesVL.cshtml" />
<None Include="AreasBackOfficeViewsHomeEVTAccomodationHotelVL.cshtml" />
<None Include="AreasBackOfficeViewsHomeEVTFoodSeasonVL.cshtml" />
<None Include="AreasBackOfficeViewsHomeEVTFoodTypeVL.cshtml" />
<None Include="AreasBackOfficeViewsHomeEVTSeasonVL.cshtml" />
<None Include="AreasBackOfficeViewsHomeEVTCountryVL.cshtml" />
<None Include="AreasBackOfficeViewsHomeEVTFreightVL.cshtml" />
<None Include="AreasBackOfficeViewsHomeEVTParticipantOriginVL.cshtml" />
<None Include="AreasBackOfficeViewsHomeEVTTechnicalProductionVL.cshtml" />
<None Include="AreasBackOfficeViewsHomeEVTTransportVL.cshtml" />
<None Include="AreasBackOfficeViewsHomeEVTTypeOfBuildingVL.cshtml" />
<None Include="AreasBackOfficeViewsHomeEVTTypeOfHeatingVL.cshtml" />
<None Include="AreasBackOfficeViewsUsersIndex.cshtml" />
<None Include="AreasReportingViewsReportsEVTVenuesEVT.cshtml" />
<None Include="AreasReportingViewsResultEvent.cshtml" />
<None Include="ViewsShared_LayoutReporting.cshtml" />
</ItemGroup>
</Project>
2
Answers
The
<None>
tags at the end of.csproj
file would instruct MSBuild to not include those files in either build or publish, that could be the possible reason of missingIndex.cshtml
View.In csproj file I found that the cshtml file was added to <content remove …> tag, removing the files from this tag solved it for me