IIS 10上的ASP.NET Core 404错误

问题描述 投票:15回答:3

我在IIS 10上运行的ASP.NET Core Web应用程序有问题。我正在使用AngularJS开发单页应用程序。

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

任何人都可以发现我如何修复后端请求?

这是我的Program.cs

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

    host.Run();
}

这是Startup.cs中的配置方法

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?}");
    });
}
c# asp.net iis asp.net-core asp.net-core-1.0
3个回答
11
投票

您的代码正在使用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

19
投票

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


6
投票

为了搜索者的利益。

使用IIS时我得到了404。我按照正确的程序发布(here)和部署为详细的here

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

基本上,在创建应用程序池时,以及将其设置为“No Managed Code”时,我还需要进入高级设置并将应用程序池标识设置为“网络服务”。在我的机器上的ApplicationPoolIdentity下它很好,但在我部署到的机器上却没有。

enter image description here

因此,为清楚起见,我的完整程序是:

要创建包:

  1. 创建dotnet核心网站(我使用Visual Studio 2017)
  2. 发布。可以使用VS的发布功能,但我通过包管理器使用了CLR。命令是: dotnet发布-c发布-r win-x64 - 自包含

我不得不使用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。右键单击“应用程序池”,然后“添加应用程序池”。创建一个.NET CLR版本设置为“无托管代码”。
  5. (我的机器不需要这个步骤,但服务器确实如此)。再次点击应用程序池。右键单击新的应用程序池,然后选择“高级设置”。将身份更改为“网络服务”(如上图所示)。
  6. 返回顶级IIS,展开“默认网站”,右键单击您网站的文件夹,然后选择“转换为应用程序”。选择没有托管代码的新应用程序池。
  7. 以admin和iisreset打开命令提示符。您应该在安装dotnet核心运行时后第一次需要这个。
  8. 访问该网站(例如http://localhost/testsite
© www.soinside.com 2019 - 2024. All rights reserved.