2010-06-29 30 views
7

我的網站有一個處理所有文件下載請求的處理程序(FileDownload.ashx)。ASP.Net 4.0 - 如何從ASHX內部訪問RouteData?

我最近將我的網站遷移到ASP.NET 4.0,現在它廣泛使用路由。與頁面請求(Java)打交道時一切正常,但它不是我的處理程序的工作 - 我遇到以下錯誤:

Type '.Handlers.FileDownload' does not inherit from 'System.Web.UI.Page'.

這是有道理的,因爲路由在頁面只執行。

我需要採取哪些步驟才能將路由和我的.ashx一起使用?我希望能夠從路線中提取RouteData.Values

public class FileDownload : IHttpHandler 
{ 
} 

回答

1

聽起來像是一個IIS問題。

如果嘗試使用ASP.NET開發服務器(Cassini),是否可以正常工作?

如果您使用IIS6,則需要使用通配符應用程序映射 - 請參閱here

你也仍然需要創建你的路由按任何ASPX頁面,如下所示:

public static void RegisterRoutes(RouteCollection routes) 
{ 
    string[] allowedMethods = { "GET", "POST" }; 
    HttpMethodConstraint methodConstraints = new HttpMethodConstraint(allowedMethods); 

    Route fileDownloadRoute = new Route("{foo}/{bar}", new FileDownload()); 
    fileDownloadRoute.Constraints = new RouteValueDictionary { { "httpMethod", methodConstraints } }; 

    routes.Add(fileDownloadRoute); 
} 

你有沒有這樣做呢?如果是這樣,我會說你的問題是定義與IIS。

請參閱here以獲取有關ASP.NET 4和IIS7路由的好文章。

祝你好運!

+0

感謝您的幫助! – 2010-06-30 13:17:23