[英]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.