c# - Asp.net MVC-从请求的 url 获取调整大小的图像得到 404 错误

标签 c# asp.net-mvc http-status-code-404

我在服务器的图像文件夹中有一些图像文件
当 url 请求的图像不存在时,我想获取图像的调整大小版本。
我编写了代码来调整原始图像的图像大小。
但我不知道当从 url 发送请求时如何执行此操作,例如
当我输入 http://example.com/Image/a_140_140.jpg 时,如果存在名称为 a.jpg 的文件,则使用我的名称创建一个调整大小的图像,名称为 a_140_140.jpg代码并将其返回给客户端。
但现在我收到 404 错误
请帮助我如何完成这项工作以避免出现 404 错误。

我不再在我的应用中添加任何路线

最佳答案

我建议使用以下解决方案:
我使用 Asp.net MVC 但经过一些更改,您可以将其用于每个站点
1. 将以下行添加到 web.config

  <httpErrors>
     <remove statusCode="404" />
  <error statusCode="404" path="/Home/Http404/" responseMode="ExecuteURL" />
  </httpErrors>

2.将此路由添加到 Routeconfig.cs

       routes.MapRoute(
            "404-PageNotFound",
            "{*url}",
            new { controller = "Home", action = "Http404" });

3.对于 Controller 代码,请使用以下代码:

 [MySite.WebUI.Infrastructure.AjaxCrawlable]
        public ActionResult Index()
        {
            string url = Request.Url.AbsolutePath;
            if (url.LastIndexOf('.') != -1)
            {
                string extention = url.Substring(url.LastIndexOf('.') + 1).ToLower();
                var extentionList = new string[] { "jpg", "png", "gif", "tif" };
                if (extentionList.Contains(extention))
                {
                    try
                    {
                        string path = System.Web.Hosting.HostingEnvironment.MapPath(Request.Url.AbsolutePath);
                        string FileName = System.IO.Path.GetFileNameWithoutExtension(path);
                        string extentionnow = System.IO.Path.GetExtension(path);
                        var _mainpath = Request.Url.AbsolutePath.Substring(0, Request.Url.AbsolutePath.LastIndexOf("/"));
                        string _mainfileName = FileName.Substring(0, (FileName.LastIndexOf('_')));
                        int _imagesize = int.Parse(FileName.Substring((FileName.LastIndexOf('_') + 1)));
                        if (_imagesize != null || _imagesize != 0)
                        {
                            var t = Resize.FindResizeImage("~" + _mainpath + "/" + _mainfileName + extentionnow, _imagesize);
                            return new RedirectResult(Request.Url.AbsolutePath);
                        }
                    }
                    catch
                    {
                        return View();
                    }

                }
            }
            else
                return View();
            return View();
        }
  • 添加此类以调整图像大小
  • 使用系统; 使用 System.Collections.Generic; 使用系统绘图; 使用 System.Drawing.Drawing2D; 使用系统.IO; 使用 System.Linq; 使用系统.Web; 使用 System.Web.Mvc;

    namespace MySite.WebUI.Models
    {
        public static class Resize
        {
            public static string FindResizeImage(string path, int size)
            {
    
                var _serverpath = HttpContext.Current.Server.MapPath(path);
                if (System.IO.File.Exists(_serverpath))
                {
                    var Extention = Path.GetExtension(path);
                    var _pathresize = path.Substring(0, path.LastIndexOf(".")) + "_" + size.ToString() + Extention;
                    var _serverpathresize = HttpContext.Current.Server.MapPath(_pathresize);
    
                    if (System.IO.File.Exists(_serverpathresize))
                    {
                        return _pathresize.Substring(1, _pathresize.Length - 1);
                    }
                    else
                    {
                        Resize.Resizeing(HttpContext.Current.Server.MapPath(path), HttpContext.Current.Server.MapPath(_pathresize), size);
                        return _pathresize;
                    }
                }
                return null;
            }
            public static void Resizeing(string imageFile, string outputFile, int size)
            {
                using (var srcImage = Image.FromFile(imageFile))
                {
                    using (var newImage = new Bitmap(size, size))
                    using (var graphics = Graphics.FromImage(newImage))
                    {
                        graphics.SmoothingMode = SmoothingMode.AntiAlias;
                        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
                        graphics.DrawImage(srcImage, new Rectangle(0, 0, size, size));
                        newImage.Save(outputFile);
                    }
                }
            }
        }
    }
    

    关于c# - Asp.net MVC-从请求的 url 获取调整大小的图像得到 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28118237/

    相关文章:

    c# - 检查文件夹是否包含具有特定扩展名的文件

    C# 异步 Ping : How to avoid an out of memory exception?

    asp.net-mvc - 将 MVC 网站部署到 Azure

    c# - ASP.NET Core GZIP 编码中间件仅在添加两次后才有效

    .htaccess - 如何使用静态 html 页面提供 HTTP header 404?

    php - 如何在 CodeIgniter 中更改 URL?

    c# - 如何使用旧的 MS Sans Serif 字体

    c# - 正则表达式在非捕获组中包含字符?

    regex - 除非明确允许,否则如何配置 Apache 重写规则以将所有请求重定向到 404

    asp.net-mvc - 我可以修改 Html.DisplayFor 以显示任何字符的子字符串吗