升级到MVC3。无法找到ashx资源。路由问题?

时间:2013-09-04 15:08:51

标签: c# asp.net asp.net-mvc asp.net-mvc-3

我已经搜索了这个问题的解决方案,但是没找到。我刚刚使用本指南将MVC2应用程序升级到MVC3:http://www.asp.net/whitepapers/mvc3-release-notes#upgrading

我还将项目从VS2008升级到VS2012。 IIS 7.5

除了我的Preview.ashx现在给我资源未找到,一切都完美无缺。当在查询字符串中使用url调用时,此页面应显示预览图像。我尝试更改路由,检查控制器名称,在Specific Page设置中设置Web等等。我很确定它与路由或某些在升级过程中搞砸的设置有关,但我无法弄清楚。

我在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

1 个答案:

答案 0 :(得分:0)

我放弃了尝试让ashx工作,最后只是在控制器中构建一个FilesResult动作来返回图像。 ashx正在接收HttpContext以处理图片并将其返回。我创建了一个采取路径,执行其工作并返回图像的动作。有一些关于.ashx文件和MVC路由的问题让我无法理解。我的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");
}