[英]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:创建包:
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:部署:
iisreset
.iisreset
身份打开命令提示符。 You should only need this the first time after you've installed the dotnet core runtime.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:
如果现在有人遇到这个问题,这就是为我解决的问题:
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-installer从https://dotnet.microsoft.com/download/dotnet-core/thank-you/runtime-aspnetcore-3.1.4-windows-hosting-bundle-installer下载并安装 dotnet-hosting-3.1.4-win
Downloaded and installed dotnet-sdk-2.2.207-win-x64 from https://dotnet.microsoft.com/download/dotnet-core/2.2从https://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.