为什么找不到我的 ASP.NET 应用程序的索引页?

问题描述 投票:0回答:1

我目前正在 ASP.NET Core MVC 项目上进行编码。一切都工作得很好,直到突然间,当我编译并启动项目时,该页面不再托管在 root 上(它总是尝试打开 localhost:7053/index.html,调用时会抛出 404)。

自从 Program.cs 或 HomeController 工作以来,我没有对它进行任何更改。

我在这里尝试学习的唯一新概念是 OAuth2,我删除了它的代码,因为,正如刚才所说,它已经全部工作了。

这是我的Program.cs

using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using System.Text;
using System.Text.Json;


var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();

// Add services to the container.
builder.Services.AddControllersWithViews();


var app = builder.Build();

app.UseCookiePolicy(new CookiePolicyOptions
{
    Secure = CookieSecurePolicy.Always,
    MinimumSameSitePolicy = SameSiteMode.None,
});

 app.UseHsts();

app.UseHttpsRedirection();
app.UseStaticFiles();

//DO NOT CHANGE
app.UseAuthentication();
app.UseRouting();
app.UseAuthorization();


app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

这就是我的索引视图功能:

        public IActionResult Index()
        {
            return View();
        }

我什至尝试了恢复 GitHub 提交更新,但错误仍然存在。一旦我托管它,一切都会顺利(部署时它工作得很好)。当提示 URL 时,可以毫无问题地访问所有其他控制器。

有人可以帮助我吗?

我已经尝试过:

  • 恢复存储库并尝试旧版本
  • 插入默认代码,还是不行
  • StackOverflow 上写的不同解决方案
c# asp.net-core-mvc
1个回答
0
投票

我使用 Razor Pages,我遇到了这个问题,它发生是因为你需要显示新目录:

将此添加到program.cs

builder.Services.AddRazorPages(options =>
{
      options.RootDirectory = "/wwwroot/Pages";
});

ps:“页面”-带有页面的目录

您可以在这里阅读

© www.soinside.com 2019 - 2024. All rights reserved.