Asp.net MVC-从请求的网址获取调整大小的图片会出现404错误

梅迪·哈格纳斯(Mehdi Haghshenas)

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

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

1988年

我提供使用以下解决方案:
我使用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.对于控制器代码,请使用以下代码:

 [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();
        }
  1. 添加此类以调整图像大小

使用系统;使用System.Collections.Generic; 使用System.Drawing; 使用System.Drawing.Drawing2D; 使用System.IO; 使用System.Linq; 使用System.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);
                }
            }
        }
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用asp.net核心MVC的任何操作都会出现404错误

来自分类Dev

使用asp.net核心MVC的任何操作都会出现404错误

来自分类Dev

在ASP.NET MVC中添加BaseController会出现错误

来自分类Dev

ASP.NET MVC 4捆绑软件出现404错误

来自分类Dev

ASP.NET MVC处理请求错误

来自分类Dev

ASP.NET MVC处理请求错误

来自分类Dev

网址路由帮助Asp.net Mvc

来自分类Dev

显示上传的图片asp.net mvc

来自分类Dev

Asp.Net MVC获取显示名称

来自分类Dev

ASP.NET MVC 5 获取声明

来自分类Dev

ASP.NET MVC jQuery Datepicker错误

来自分类Dev

ASP .Net MVC中的错误处理

来自分类Dev

ASP.NET MVC jQuery发布错误

来自分类Dev

ASP .NET MVC OutputCache错误的位置

来自分类Dev

asp.net mvc 中的 UnitOfWork 错误

来自分类Dev

在ASP.NET MVC视图中获取相对网址

来自分类Dev

ASP.NET MVC API PUT收到404错误

来自分类Dev

asp.net MVC + RouteLink仅收到404错误

来自分类Dev

Sentry 未记录 ASP.NET MVC 站点的 404 错误

来自分类Dev

上载图片asp.net MVC时出现Base64错误

来自分类Dev

获取NullRefence异常错误C#asp.net MVC

来自分类Dev

从银行网站回调到我在ASP.NET MVC中的操作时出现404错误

来自分类Dev

禁用ASP.NET MVC路由以进行网址解析

来自分类Dev

ASP.NET MVC5 OWIN拒绝长网址

来自分类Dev

在ASP.NET MVC App中解码网址参数值

来自分类Dev

ASP.NET MVC4中页面的网址

来自分类Dev

无法使用Formdata Asp.net-mvc上传图片

来自分类Dev

图片上传MVC / Javascript / ASP.NET C#

来自分类Dev

ASP.NET MVC 背景图片