简体   繁体   English

ASP.NET 核心 404 错误 IIS 10

[英]ASP.NET Core 404 Error on IIS 10

I have a problem with ASP.NET Core web application running on IIS 10. I am developing a Single Page Application with AngularJS.我在 IIS 10 上运行的 ASP.NET Core web 应用程序有问题。我正在开发一个带有 AngularJS 的单页应用程序。

The index.html loads perfectly but the backend requests are failing with 404 error code on the IIS 10. From Visual Studio with IIS Express it works perfectly. index.html 加载完美,但后端请求在 IIS 10 上失败并显示 404 错误代码。从带有 IIS Express 的 Visual Studio 中,它完美运行。

Can anyone spot how can I fix the backend requests?谁能发现我该如何修复后端请求?

Here's my Program.cs这是我的 Program.cs

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

    host.Run();
}

And here's my Configure method from Startup.cs这是我在 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?}");
    });
}

就我而言,问题是我的控制器抛出异常,因此框架尝试使用不可用的异常处理程序页面,因此出现 404 错误,控制器本身抛出 500 错误

For the benefit of searchers.为了搜索者的利益。

I was getting a 404 when Using IIS.使用 IIS 时出现 404。 I had followed the correct procedure for publishing ( here ) and deployment as detailed here .我遵循了正确的发布程序( 此处)和部署,如此处所述

It took some time to figure out, but I eventually found the answer hidden in a Rick Strahl blog post .花了一些时间才弄明白,但我最终找到了隐藏在Rick Strahl 博客文章中的答案。

Basically, when making the application pool, as well as setting it to 'No Managed Code', I also needed to go into the advanced settings and set the Application Pool Identity to 'Network Service'.基本上,在制作应用程序池并将其设置为“无托管代码”时,我还需要进入高级设置并将应用程序池标识设置为“网络服务”。 It was fine under ApplicationPoolIdentity on my machine, but not on a machine I deployed to.在我的机器上的 ApplicationPoolIdentity 下很好,但在我部署到的机器上却没有。

在此处输入图片说明

So, for clarity, my full procedure was:所以,为了清楚起见,我的完整程序是:

To create package:创建包:

  1. Create dotnet core website (I used Visual Studio 2017)创建 dotnet 核心网站(我使用的是 Visual Studio 2017)
  2. Publish.发布。 Could have used VS's publish function, but I used CLR via the package manager.本来可以使用 VS 的发布功能,但我通过包管理器使用了 CLR。 The command was:命令是:

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

I had to use the win-x64 identifier as we have to be compatible with 64-bit Windows Server 2008.我必须使用win-x64 标识符,因为我们必须与 64 位 Windows Server 2008 兼容。

To deploy:部署:

  1. Make a folder in C:\\inetpub\\wwwroot (eg 'testsite')在 C:\\inetpub\\wwwroot 中创建一个文件夹(例如“testsite”)
  2. Take the contents of the publish folder ({root}\\bin\\Release\\netcoreapp2.1\\win-x64\\publish) and copy it to the new 'testsite' folder (or your equivalent).获取发布文件夹 ({root}\\bin\\Release\\netcoreapp2.1\\win-x64\\publish) 的内容并将其复制到新的“testsite”文件夹(或您的等效文件夹)。
  3. Install the dotnet core runtime (not SDK!) on the host machine.在主机上安装 dotnet 核心运行时(不是 SDK!)。
  4. Open IIS.打开 IIS。 Right click 'Application pools', then 'Add Application Pool'.右键单击“应用程序池”,然后单击“添加应用程序池”。 Create one with the .NET CLR Version set to 'No Managed Code'.创建一个将 .NET CLR 版本设置为“无托管代码”的文件。
  5. (my machine didn't need this step, but server did).Click on Application pools again. (我的机器不需要这一步,但服务器需要)。再次单击应用程序池。 Right click your new App Pool and Choose 'Advanced Settings'.右键单击您的新应用程序池并选择“高级设置”。 Change the identity to 'Network Service' (as shown in the picture above).将身份更改为“网络服务”(如上图所示)。
  6. Back at top level IIS, Expand 'Default Web Site', right click the folder of your website and choose 'Convert to application'.返回顶级 IIS,展开“默认网站”,右键单击您网站的文件夹并选择“转换为应用程序”。 Choose your new App Pool with No Managed Code.选择没有托管代码的新应用程序池。
  7. Open command prompt as admin and iisreset .以 admin 和iisreset身份打开命令提示符。 You should only need this the first time after you've installed the dotnet core runtime.安装 dotnet 核心运行时后,您应该只在第一次需要它。
  8. Visit the site (eg http://localhost/testsite )访问该站点(例如http://localhost/testsite

You code is working on my machine with Kestrel.您的代码正在使用 Kestrel 在我的机器上运行。 A good troubleshooting step is to find out whether the problem is with your ASP.NET Core application or with your IIS Hosting configuration.一个很好的故障排除步骤是找出问题是与您的 ASP.NET Core 应用程序还是与您的 IIS 托管配置有关。

Try this from the root of your project.从项目的根目录尝试此操作。

dotnet restore
dotnet run

You will see something like this:你会看到这样的事情:

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

In your browser go to the following two URLs.在您的浏览器中转到以下两个 URL。 If they don't work, then something is wrong with your application.如果它们不起作用,则说明您的应用程序有问题。 If they do work, something is wrong with your IIS hosting.如果它们确实有效,则说明您的 IIS 托管有问题。

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

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

Had the same issue now which brought me to this page.现在遇到了同样的问题,这让我来到了这个页面。 In case anyone is having this issue now this was what fixed it for me:如果现在有人遇到这个问题,这就是为我解决的问题:

  1. Downloaded and installed dotnet-hosting-3.1.4-win from https://dotnet.microsoft.com/download/dotnet-core/thank-you/runtime-aspnetcore-3.1.4-windows-hosting-bundle-installerhttps://dotnet.microsoft.com/download/dotnet-core/thank-you/runtime-aspnetcore-3.1.4-windows-hosting-bundle-installer下载并安装 dotnet-hosting-3.1.4-win

  2. Downloaded and installed dotnet-sdk-2.2.207-win-x64 from https://dotnet.microsoft.com/download/dotnet-core/2.2https://dotnet.microsoft.com/download/dotnet-core/2.2下载并安装 dotnet-sdk-2.2.207-win-x64

If you are using an api call make sure you class has:如果您使用 api 调用,请确保您的课程具有:

[Route("api/[controller]")] [ApiController] [Route("api/[controller]")] [ApiController]

above where the class is instantiated.上面是类被实例化的地方。

Also make sure you have: [HttpGet()] for example above the api method you are trying to debug.还要确保您有: [HttpGet()] 例如在您尝试调试的 api 方法上方。

For me the issue was an ISAPI Filter called UrlScan.对我来说,问题是一个名为 UrlScan 的 ISAPI 过滤器。 I had to delete it from the application, only then did it work.我不得不从应用程序中删除它,然后才起作用。

I'm a noob with windows IIS deployments so this answer might seem obvious to some people.我是 Windows IIS 部署的菜鸟,所以这个答案对某些人来说似乎很明显。

I was missing a key file - web.config which IIS needs in order to run your web application.我缺少一个关键文件 - web.config ,IIS 需要它来运行您的 Web 应用程序。 I used the web.config configurations specified here:我使用了此处指定的web.config配置:

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

Specifically, the configurations for ASP.NET Core Module.具体来说,ASP.NET Core 模块的配置。

in my case, the problem was fixed by setting environment variable in the web.config在我的情况下,通过在 web.config 中设置环境变量来解决问题

<aspNetCore processPath="dotnet"
      arguments=".\MyApp.dll"
      stdoutLogEnabled="false"
      stdoutLogFile=".\logs\stdout"
      hostingModel="inprocess">
  <environmentVariables>
    <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
     </environmentVariables>
</aspNetCore>

I had configured a root directory using .AddRazorPageOptions(rpo => rpo.RootDirectory = "/Folder") , but I forgot and my .cshtml files were still in the root of the project instead.我已经使用.AddRazorPageOptions(rpo => rpo.RootDirectory = "/Folder")配置了一个根目录,但我忘记了,我的.cshtml文件仍然在项目的根目录中。 I just had to make the /Folder folder and put my files in there and it worked.我只需要创建/Folder文件夹并将我的文件放在那里就可以了。

I was missing a handler mapping for AspNetCoreModuleV2.我缺少 AspNetCoreModuleV2 的处理程序映射。

Request Path: * Module: AspNetCoreModuleV2 Executable: blank Name: aspNetCore (this I believe can be anything you want)请求路径:* 模块:AspNetCoreModuleV2 可执行文件:空白 名称:aspNetCore(我相信这可以是任何你想要的)

Under Request Restrictions, I unchecked the "Invoke handler only if request is mapped to:" checkbox.在请求限制下,我取消选中“仅当请求映射到时调用处理程序:”复选框。 Selected All verbs, and Script access.选择所有动词和脚本访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM