【发布时间】:2016-07-22 14:35:14
【问题描述】:
我遇到了在 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?}");
});
}
【问题讨论】:
-
启用 ASP.NET Core 日志记录(可能带有调试日志级别)并查看是否有任何消息说明失败的原因
-
也发布您的 web.config。
-
你配置好MVC服务了吗?
-
你是否添加了一个带有 Index 操作的 Home 控制器?
标签: c# asp.net iis asp.net-core asp.net-core-1.0