2016-04-28 16 views
0
擔任

我已經成功地建立了單(穩定4.2.3.4)在Ubuntu 14.04與NGINX(1.8.1)主辦ASP.NET MVC應用程序,它很好。我唯一無法解決的是「索引/根(/)404」問題。單聲道和Nginx的:指數(/)404通過StaticFileHadler

我使用自定義的CustomMvcRouteHandler爲包羅萬象的路線:

routes.MapRoute(
    "UrlHandler", // Route name 
    "{*url}", 
    null, 
    null, 
    new[] { "web.Controllers" } 
    ).RouteHandler = new CustomMvcRouteHandler(); 

這個偉大的工程,所有預期請求通過CustomMvcRouterHandler路由(); 但不適用於索引(/)。由於任何原因,索引請求(/)由StaticFileHandler提供並引發此錯誤:

System.Web.HttpException:找不到資源。

說明: HTTP 404.您正在查找的資源(或其某個依賴項)可能已被刪除,名稱已更改或暫時不可用。請檢查以下網址並確保它拼寫正確。

詳情:請求的URL:/

異常堆棧跟蹤:

at System.Web.StaticFileHandler.ProcessRequest (System.Web.HttpContext context) <0x407e59f0 + 0x00753> in <filename unknown>:0 
    at System.Web.DefaultHttpHandler.BeginProcessRequest (System.Web.HttpContext context, System.AsyncCallback callback, System.Object state) <0x407e5720 + 0x00153> in <filename unknown>:0 
    at System.Web.HttpApplication+<Pipeline>c__Iterator1.MoveNext() <0x408c5000 + 0x04485> in <filename unknown>:0 
    at System.Web.HttpApplication.Tick() <0x408c2730 + 0x00057> in <filename unknown>:0 

請注意,它僅適用於單就這樣,我的Windows開發機器上,甚至在任何Windows IIS使用相同的web.config託管相同的Web應用程序等,即使對於根(/)請求也可以正常工作,並且這些請求將通過CustomMvcRouteHandler()進行路由。

我真的不明白的是爲什麼Mono使用StaticFileHandler服務索引(/)。如果我在Web結構的根目錄中創建index.html文件,那麼它將由StaticFileHandler提供服務(如預期的那樣,因爲靜態文件處理程序正在查找該文件)。

這是該站點的NGINX配置文件:

server { 
    listen 80; 
    listen [::]:80; 
    server_name example.com; 
    access_log /var/www/example.com/logs/access.log; 
    error_log /var/www/example.com/logs/error.log; 

    include /etc/nginx/fastcgi_params; 

    location/{ 
     fastcgi_index /; 
     root /var/www/example.com/www/; 
     fastcgi_pass 127.0.0.1:9000; 
    } 
} 

我現在使用的解決方法是chaning行:

 fastcgi_index /; 

 fastcgi_index /Default.aspx; 

這樣,根訪問權限(/)正在更改爲/Default.aspx並通過ASP.NET MVC管道進行路由,但我當然需要ch代碼變得憤怒並將/Default.aspx視爲/。

任何想法?

回答

0

發佈問題後發現解決方案小時後,該死的。

問題出在Catchall路由規範並且「defaults」參數爲NULL。以這種方式更改它可以解決問題:

routes.MapRoute(
    "UrlHandler", // Route name 
     url: "{*url}", 
     defaults: new { controller = "Home", action = "Index" }, 
     constraints: null, 
     namespaces: new[] { "web.Controllers" } 
    ).RouteHandler = new CustomMvcRouteHandler(); 

請參閱controller =「Home」,action =「Index」的默認值。即使索引(/)路由由ASP.NET MVC管道提供,也具有指定的默認值。

這樣做的副作用是,將「控制器」和「操作」的RouteData.Values添加到默認值中指定的集合中,因此如果您想將它們添加到代碼中(如同我),異常將會拋出:

具有相同鍵的項目已被添加。

所以最好檢查並刪除這些值,如果需要的話。

if (requestContext.RouteData.Values.ContainsKey("controller")) 
    requestContext.RouteData.Values.Remove("controller"); 
if (requestContext.RouteData.Values.ContainsKey("action")) 
    requestContext.RouteData.Values.Remove("action"); 

P.S:這是所有關於那些託管在IIS上對單的時候:)小的差異。