gpt4 book ai didi

asp.net-mvc - MVC 3 使用具有不同扩展名的自定义 httphandler

转载 作者:行者123 更新时间:2023-12-01 10:09:59 25 4
gpt4 key购买 nike

您可以在 MVC 中创建带有自定义扩展的自定义 httphandler 吗?

我想要一个具有以下路径的图像处理程序(如果可能),domain.com/picture/{图片的id}

是否可以从 MVC 内部进行,还是必须在 IIS 7 中进行?

最佳答案

无需添加 httphandler。您应该通过 Controller 在 asp.net mvc 中执行此操作示例:

public class PictureController : Controller
{
public FileResult GetImage(int pictureID)
{
byte[] fileContents = null;
//Get the file here.
return File(fileContents, "image/jpeg");
}
}

在global.asax中你可以定义

routes.MapRoute("Picture-GetImage", "picture/{pictureID}",
new { controller = "Picture", action = "GetImage" }

你也可以使用 System.Web.Helpers.WebImage 助手,或者手动完成,例如:

public static byte[] ProcessCropResizeImage(string imageurl, Size outputSize)
{
if (File.Exists(imageurl))
{
MemoryStream result = new MemoryStream();
ImageCodecInfo codec = ImageCodecInfo.GetImageEncoders().FirstOrDefault(m => m.MimeType == "image/jpeg");
if (codec == null)
throw new ArgumentException(string.Format("Unsupported mimeType specified for encoding ({0})", "image/jpeg"), "encodingMimeType");
EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 85L);
using (FileStream fs = File.OpenRead(imageurl))
{
using (Image image = Image.FromStream(fs))
{
using (Bitmap b = new Bitmap(outputSize.Width, outputSize.Height))
{
using (Graphics g = Graphics.FromImage((Image)b))
{
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(image, 0, 0, outputSize.Width, outputSize.Height);
g.DrawImage(image, outputSize.Width, outputSize.Height);
}
b.Save(result, codec, encoderParams);
}
}
}

return result.GetBuffer();
}
return null;
}

关于asp.net-mvc - MVC 3 使用具有不同扩展名的自定义 httphandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6028348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com