簡體   English   中英

升級到MVC3。 找不到ashx資源。 路由問題?

[英]Upgrade to MVC3. ashx resource cannot be found. Routing issue?

我在高空搜尋了一個解決此問題的方法,但沒有找到一個。 我使用以下指南將MVC2應用程序升級到了MVC3: http : //www.asp.net/whitepapers/mvc3-release-notes#upgrading

我還將項目從VS2008升級到VS2012。 IIS 7.5

一切都進行得很順利,除了我的Preview.ashx現在為我提供了找不到的資源。 當在查詢字符串中使用url調用時,該頁面應該顯示預覽圖像。 我嘗試更改路由,檢查控制器名稱,在Web設置中設置“ Specific Page ”等。我很確定它與路由或升級期間搞砸的某些設置有關,但我無法弄清楚。

我在http://localhost/comm IIS中使用虛擬目錄進行了站點設置

編輯:我只是使用MVC3的全新安裝重建了站點,但問題仍然存在。 重建站點后,我意識到同一目錄中存在.aspx文件,它們工作正常。 只是.ashx文件無法正確路由。

Global.asax

public static void RegisterRoutes(RouteCollection routes) {
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
    routes.IgnoreRoute("{resource}.ashx/{*pathInfo}");

    routes.MapRoute(
        "Default", // Route name
        "{instance}/{controller}/{action}/{id}", // URL with parameters
        new { instance = "demo", controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

    }

    protected void Application_Start() {
            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);
    }

錯誤

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

Requested URL: /comm/Views/Review/FileViewer.ashx

我放棄了讓ashx正常工作的嘗試,最終只在返回圖像的控制器中構建了一個FilesResult操作。 ashx接收HttpContext以便處理圖像並返回它。 我創建了一個采取路徑,執行其工作並返回圖像的動作。 .ashx文件和MVC路由有些.ashx ,我無法弄清楚。 我的ashx文件都無法正常工作,但是可以將它們全部重建為動作,因此我認為這沒什么大不了的。 下面是替換Preview.ashx

public FileResult Preview(string path) {
// Database fetch of image details
try {
    // Get the relative path
    if (!path.IsNull()) {
        // Get the path
        string filePath = path;
        string absolutePath = ...

        // Make sure the the file exists
        if (System.IO.File.Exists(absolutePath)) {
            // Check the preview
            string previewPath = ...;

            // Has preview
            bool hasPreview = true;
            // Check to see if the preview exists
            if (!System.IO.File.Exists(previewPath) || System.IO.File.GetLastWriteTime(previewPath) < System.IO.File.GetLastWriteTime(absolutePath)) {
                try {
                    // Generate preview
                    hasPreview = ...  != null;
                }
                catch (Exception exc) {
                    hasPreview = false;
                }
            }

            // Once the path is handled, set the type
            if (hasPreview) {
                return new FileStreamResult(new FileStream(previewPath, FileMode.Open), "image/png");
            }
            // No preview
            else
                return WriteDefault();
        }
        else
            // Write the default (blank)
            return WriteDefault();
    }
    else {
        return WriteDefault();
    }
}
catch {
    // Write the default (no_photo)
    return WriteDefault();
    }
}

private FileContentResult WriteDefault() {
    // Write the default
    System.Drawing.Bitmap bmp = new Bitmap(25, 25);
    System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp);
    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    g.FillRectangle(System.Drawing.Brushes.White, 0, 0, 25, 25);
    bmp = new Bitmap(25, 25, g);
    MemoryStream str = new MemoryStream();
    bmp.Save(str, ImageFormat.Png);
    return File(str.ToArray(), "image/png");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM