asp.net 找不到视图,即使它存在

b5lpy0ml  于 2023-10-21  发布在  .NET
关注(0)|答案(1)|浏览(174)

我在从控制器到视图的过程中遇到了问题。我有一个错误,它告诉我没有找到视图,除了它存在并且它在正确的位置。
奇怪的是,当我在本地添加.AddRazorRuntimeCompilation();那就成功了但是如果我删除这个部分或在生产中测试,那么它就不再工作了。
这里是树:

错误代码:

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

控制员:

[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();

我的印象是,这是因为我有不同的区域,但视图位于错误告诉我要寻找的位置。
我是不是漏了什么?(我用的是NET.7)
编辑:
CSproj文件:

<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>
esbemjvw

esbemjvw1#

.csproj文件末尾的<None>标记将指示MSBuild在构建或发布中不包含这些文件,这可能是缺少Index.cshtml View的可能原因。

相关问题