I have searched high and low for a solution to this problem and have not found one. I just upgraded a MVC2 application to MVC3 using this guide: http://www.asp.net/whitepapers/mvc3-release-notes#upgrading
I also upgraded the project from VS2008 to VS2012. IIS 7.5
Everything went flawlessly except my Preview.ashx is now giving me resource not found. This page is supposed to display a preview image when called with a url in the query string. I tried changing routes, checking the controller names, setting Specific Page
in the Web
settings, etc. I am pretty sure it has something to do with the routes or some setting that got screwed up during the upgrade, but I cannot figure it out.
I have the site setup using a virtual directory in IIS at http://localhost/comm
EDIT: I just rebuilt the site using a fresh installation of MVC3 and the problem still exists. After rebuilding the site I realized that there are .aspx files in the same directory that work fine. It is only .ashx files that are not routing correctly.
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);
}
Error
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
I gave up trying to get the ashx
to work and ended up just building a FilesResult
action in the controller that returns the image. The ashx
was receiving an HttpContext
in order to process the image and return it. I created an action that takes a path, performs its work, and returns the image. There is something wonky about .ashx
files and MVC routing that I could not figure out. None of my ashx
files are working, but they can all be rebuilt as actions so I guess its not a big deal. Below is the action that replaces the 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");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.