6

【茶包射手日記】ASP.NET MVC 網站 Controller 網址傳回 HTTP 404

 2 years ago
source link: https://blog.darkthread.net/blog/aspnet-mvc-precompiledapp/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
【茶包射手日記】ASP.NET MVC 網站 Controller 網址傳回 HTTP 404-黑暗執行緒

同事遇到的問題 - 某 ASP.NET MVC 專案使用預先編譯發行,檔案部署到 IIS,設好 Web Application,檢視 css、js、png 等靜態檔 OK,但 Home/Index 等 MVC 路徑全數失效,一律傳回 HTTP 404。

經對照調查,問題關鍵在部署時漏放 PrecompiledApp.config。PrecompiledApp.config 我在 WebSite 專案時代交手過,知道它用來通知 ASP.NET Runtime 網站程式為預先編譯過的版本,App_Code 已編成 .dll,但它為什麼會造成 MVC 路由失效,產生 HTTP 404?

建個 ASP.NET MVC 專案實地觀察,發行選項有個 Precompile during publishing / 在發行期間先行編譯:

未勾選時發行產生檔案如下:

勾選先行編譯後產生檔案如下:

比較二者的差別,先行編譯版沒有 Global.asax,bin 則多出 App_global.asax.dll 及 App_global.asax.compiled,根目錄則多了 PrecompiledApp.config。IIS 執行時,若有 Global.asax,會由其內容 <%@ Application Codebehind="Global.asax.cs" Inherits="MVCTest.MvcApplication" Language="C#" %> 得到提示由 MVCTest.dll 執行 Global.asax.cs 邏輯;若有 ProcompiledApp.config 則是從 bin/App_global.asax.dll 找到 Global.asax.cs 程式並執行。當 Global.asax 跟 PrecompiledApp.config 都不存在,Global.asax.cs 裡的程式碼 便不會被執行。那,Global.asax.cs 裡有什麼?

薑薑薑講~~~ MVC 路由是在這裡註冊的,沒執行自然不知道要將 /Home/Index 導向 HomeController,HTTP 404 現象得到合理解釋,結案。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK