ASP.NET IIS 10 上的核心 404 错误

ASP.NET Core 404 Error on IIS 10

我在 IIS 10 上使用 ASP.NET 核心 Web 应用程序 运行 时遇到问题。 我正在使用 AngularJS.

开发单页应用程序

index.html 加载完美,但后端请求在 IIS 10 上失败并显示 404 错误代码。从 Visual Studio 使用 IIS Express 它完美运行。

谁能发现我该如何修复后端请求?

这是我的 Program.cs

public static void Main(string[] args)
{
    var host = new WebHostBuilder()
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .Build();

    host.Run();
}

这是我在 Startup.cs

中的 Configure 方法
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    app.UseDefaultFiles();

    app.UseStaticFiles();

    app.UseIdentity();

    // Custom middleware for Angular UI-Router
    app.Use(async (context, next) =>
    {
        if (!Path.HasExtension(context.Request.Path.Value)
        && context.Request.HttpContext.Request.Headers["X-Requested-With"] != "XMLHttpRequest"
        && context.Request.Method.ToUpper() != "POST"
        && context.Request.Method.ToUpper() != "PUT"
        && context.Request.Method.ToUpper() != "DELETE")
        {
            await context.Response.WriteAsync(File.ReadAllText(env.WebRootPath + "/index.html"));
        }

        await next();
    });

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "api/{controller=Home}/{action=Index}/{id?}");
    });
}

你的代码在我的机器上用 Kestrel 运行。一个很好的故障排除步骤是查明问题是出在您的 ASP.NET 核心应用程序上还是出在您的 IIS 主机配置上。

从项目的根目录开始尝试。

dotnet restore
dotnet run

你会看到这样的东西:

Hosting environment: Production
Content root path: C:\MyApplicationPath
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

在您的浏览器中转到以下两个 URL。如果它们不起作用,则说明您的应用程序有问题。如果它们确实有效,则说明您的 IIS 托管有问题。

localhost:5000        // you will see your index.html page
localhost:5000/api    // you will see your default routes output

在我的例子中,问题是我的控制器抛出异常,因此框架试图使用不可用的异常处理程序页面,因此出现 404 错误,控制器本身抛出 500 错误

为了搜索者的利益。

我在使用 IIS 时收到 404。我遵循了正确的发布程序 (here) and deployment as detailed .

我花了一些时间才弄清楚,但我最终找到了隐藏在Rick Strahl blog post中的答案。

基本上,在创建应用程序池并将其设置为 'No Managed Code' 时,我还需要进入高级设置并将应用程序池标识设置为 'Network Service'。在我机器上的 ApplicationPoolIdentity 下没问题,但在我部署到的机器上不行。

所以,为了清楚起见,我的完整程序是:

创建包:

  1. 创建dotnet核心网站(我用的是Visual Studio2017)
  2. 发布。本来可以使用 VS 的发布功能,但我通过包管理器使用了 CLR。命令是:

    dotnet publish -c Release -r win-x64 --self-contained

我必须使用 win-x64 identifier,因为我们必须与 64 位 Windows Server 2008 兼容。

要部署:

  1. 在 C:\inetpub\wwwroot 中创建一个文件夹(例如 'testsite')
  2. 获取发布文件夹 ({root}\bin\Release\netcoreapp2.1\win-x64\publish) 的内容并将其复制到新的 'testsite' 文件夹(或等效文件夹)。
  3. 在主机上安装 dotnet 核心 runtime(不是 SDK!)。
  4. 打开 IIS。右键单击 'Application pools',然后单击 'Add Application Pool'。创建一个将 .NET CLR 版本设置为 'No Managed Code'.
  5. (我的机器不需要这一步,但服务器需要)。再次单击应用程序池。右键单击您的新应用程序池并选择 'Advanced Settings'。将身份更改为'Network Service'(如上图所示)
  6. 返回顶级 IIS,展开 'Default Web Site',右键单击您网站的文件夹并选择 'Convert to application'。选择没有托管代码的新应用程序池。
  7. 以管理员身份打开命令提示符并iisreset。您应该只在安装 dotnet 核心运行时后第一次需要它。
  8. 访问网站(例如 http://localhost/testsite

另一个非常愚蠢但可能的变体是,由于错误的部署设置,应用程序部署在与您期望的不同的文件夹中(例如:您的服务器根而不是 sub-folder)。

现在遇到了同样的问题,所以我来到了这个页面。如果现在有人遇到这个问题,这就是为我解决的问题:

  1. 已从 https://dotnet.microsoft.com/download/dotnet-core/thank-you/runtime-aspnetcore-3.1.4-windows-hosting-bundle-installer

  2. 下载并安装 dotnet-hosting-3.1.4-win
  3. 已从 https://dotnet.microsoft.com/download/dotnet-core/2.2

  4. 下载并安装 dotnet-sdk-2.2.207-win-x64

如果您使用的是 api 电话,请确保您 class 拥有:

[路线(api/[控制器]")] [API控制器]

上面 class 实例化的地方。

还要确保你有: [HTTPGet()] 例如,在您尝试调试的 api 方法上方。

对我来说,问题是名为 UrlScan 的 ISAPI 过滤器。我不得不从应用程序中删除它,然后才起作用。

我是 windows IIS 部署的菜鸟,所以这个答案对某些人来说似乎显而易见。

我缺少一个密钥文件 - web.config IIS 需要它才能 运行 您的 Web 应用程序。我使用了此处指定的 web.config 配置:

https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/web-config?view=aspnetcore-5.0

具体来说,ASP.NET核心模块的配置。