我在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?}");
});
}
您的代码正在使用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)和部署为详细的here。
花了一些时间才弄清楚,但我最终找到了隐藏在Rick Strahl blog post中的答案。
基本上,在创建应用程序池时,以及将其设置为“No Managed Code”时,我还需要进入高级设置并将应用程序池标识设置为“网络服务”。在我的机器上的ApplicationPoolIdentity下它很好,但在我部署到的机器上却没有。
因此,为清楚起见,我的完整程序是:
要创建包:
我不得不使用win-x64 identifier,因为我们必须与64位Windows Server 2008兼容。
部署:
iisreset
打开命令提示符。您应该在安装dotnet核心运行时后第一次需要这个。