Search code examples
asp.netasp.net-mvcasp.net-core

View not found even though it exists


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 :

enter image description here

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="..\DataAccess\DataAccess.csproj" />
    <ProjectReference Include="..\Utility\Utility.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="Areas\BackOffice\Views\Home\EVT\AccomodationDistanceVL.cshtml" />
    <Content Remove="Areas\BackOffice\Views\Home\EVT\AccomodationHotelVL.cshtml" />
    <Content Remove="Areas\BackOffice\Views\Home\EVT\CountryVL.cshtml" />
    <Content Remove="Areas\BackOffice\Views\Home\EVT\FoodCategoryVL.cshtml" />
    <Content Remove="Areas\BackOffice\Views\Home\EVT\FoodDishesVL.cshtml" />
    <Content Remove="Areas\BackOffice\Views\Home\EVT\FoodSeasonVL.cshtml" />
    <Content Remove="Areas\BackOffice\Views\Home\EVT\FoodTypeVL.cshtml" />
    <Content Remove="Areas\BackOffice\Views\Home\EVT\FreightVL.cshtml" />
    <Content Remove="Areas\BackOffice\Views\Home\EVT\SeasonVL.cshtml" />
    <Content Remove="Areas\BackOffice\Views\Home\EVT\technicalProductionVL.cshtml" />
    <Content Remove="Areas\BackOffice\Views\Home\EVT\TypeOfBuildingVL.cshtml" />
    <Content Remove="Areas\BackOffice\Views\Home\EVT\TypeOfHeatingVL.cshtml" />
    <Content Remove="Areas\BackOffice\Views\Home\ParticipantOriginVL.cshtml" />
    <Content Remove="Areas\BackOffice\Views\Home\TransportVL.cshtml" />
    <Content Remove="Areas\BackOffice\Views\Users\Index.cshtml" />
    <Content Remove="Areas\Reporting\Views\Reports\Event.cshtml" />
    <Content Remove="Areas\Reporting\Views\Reports\EVT\VenuesEVT.cshtml" />
    <Content Remove="Views\Shared\_LayoutReporting.cshtml" />
  </ItemGroup>

  <ItemGroup>
    <Watch Remove="Areas\BackOffice\Views\Home\EVT\AccomodationDistanceVL.cshtml" />
    <Watch Remove="Areas\BackOffice\Views\Home\EVT\AccomodationHotelVL.cshtml" />
    <Watch Remove="Areas\BackOffice\Views\Home\EVT\CountryVL.cshtml" />
    <Watch Remove="Areas\BackOffice\Views\Home\EVT\FoodCategoryVL.cshtml" />
    <Watch Remove="Areas\BackOffice\Views\Home\EVT\FoodDishesVL.cshtml" />
    <Watch Remove="Areas\BackOffice\Views\Home\EVT\FoodSeasonVL.cshtml" />
    <Watch Remove="Areas\BackOffice\Views\Home\EVT\FoodTypeVL.cshtml" />
    <Watch Remove="Areas\BackOffice\Views\Home\EVT\FreightVL.cshtml" />
    <Watch Remove="Areas\BackOffice\Views\Home\EVT\SeasonVL.cshtml" />
    <Watch Remove="Areas\BackOffice\Views\Home\EVT\technicalProductionVL.cshtml" />
    <Watch Remove="Areas\BackOffice\Views\Home\EVT\TypeOfBuildingVL.cshtml" />
    <Watch Remove="Areas\BackOffice\Views\Home\EVT\TypeOfHeatingVL.cshtml" />
    <Watch Remove="Areas\BackOffice\Views\Home\ParticipantOriginVL.cshtml" />
    <Watch Remove="Areas\BackOffice\Views\Home\TransportVL.cshtml" />
    <Watch Remove="Areas\BackOffice\Views\Users\Index.cshtml" />
    <Watch Remove="Areas\Reporting\Views\Reports\Event.cshtml" />
    <Watch Remove="Areas\Reporting\Views\Reports\EVT\VenuesEVT.cshtml" />
    <Watch Remove="Areas\Reporting\Views\Reports\EVT\_LiveEVTCO2Result.cshtml" />
    <Watch Remove="Views\Shared\_LayoutReporting.cshtml" />
  </ItemGroup>

  <ItemGroup>
    <None Include="Areas\BackOffice\Views\Home\EVT\AccomodationDistanceVL.cshtml" />
    <None Include="Areas\BackOffice\Views\Home\EVT\FoodCategoryVL.cshtml" />
    <None Include="Areas\BackOffice\Views\Home\EVT\FoodDishesVL.cshtml" />
    <None Include="Areas\BackOffice\Views\Home\EVT\AccomodationHotelVL.cshtml" />
    <None Include="Areas\BackOffice\Views\Home\EVT\FoodSeasonVL.cshtml" />
    <None Include="Areas\BackOffice\Views\Home\EVT\FoodTypeVL.cshtml" />
    <None Include="Areas\BackOffice\Views\Home\EVT\SeasonVL.cshtml" />
    <None Include="Areas\BackOffice\Views\Home\EVT\CountryVL.cshtml" />
    <None Include="Areas\BackOffice\Views\Home\EVT\FreightVL.cshtml" />
    <None Include="Areas\BackOffice\Views\Home\EVT\ParticipantOriginVL.cshtml" />
    <None Include="Areas\BackOffice\Views\Home\EVT\TechnicalProductionVL.cshtml" />
    <None Include="Areas\BackOffice\Views\Home\EVT\TransportVL.cshtml" />
    <None Include="Areas\BackOffice\Views\Home\EVT\TypeOfBuildingVL.cshtml" />
    <None Include="Areas\BackOffice\Views\Home\EVT\TypeOfHeatingVL.cshtml" />
    <None Include="Areas\BackOffice\Views\Users\Index.cshtml" />
    <None Include="Areas\Reporting\Views\Reports\EVT\VenuesEVT.cshtml" />
    <None Include="Areas\Reporting\Views\Result\Event.cshtml" />
    <None Include="Views\Shared\_LayoutReporting.cshtml" />
  </ItemGroup>

</Project>

Solution

  • 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 missing Index.cshtml View.