2016-07-22 170 views
5

我在IIS 10上运行的ASP.NET Core Web应用程序出现问题。 我正在开发一个带有AngularJS的单页面应用程序。IIS 10上的ASP.NET核心404错误

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?}"); 
    }); 
} 
+0

启用ASP.NET Core日志记录(可能带有调试日志级别),并查看是否有任何消息说明此失败原因 –

+0

也发布了web.config。 –

+0

你配置了MVC服务吗? –

回答

3

您代码工作在我的机器用红隼上。一个很好的故障排除步骤是找出问题出在您的ASP.NET Core应用程序还是您的IIS Hosting配置中。

尝试从您的项目的根源。

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. 

在浏览器转到以下两个网址。如果他们不工作,那么你的应用程序有问题。如果他们工作,您的IIS托管有些问题。

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

谢谢,我可以修复它。问题出在我的应用程序上,但如果应用程序出现问题,那么抛出404错误有点奇怪。 –

+1

应用程序出了什么问题?你的修复涉及什么? –

+1

问题是缺少配置文件。我有3个环境appsettings文件的开发,舞台和生产与必要的连接字符串。默认发布选项仅包含appsettings.json。所以我需要将值更改为appsettings * .json以复制所有环境设置。 –

6

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