2016-07-22 172 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錯誤