如何将ASP.NET MVC 3中的自定义HttpHandler改写为支持多种文件扩展名的长尾?
- 内容介绍
- 文章标签
- 相关推荐
本文共计582个文字,预计阅读时间需要3分钟。
你可以在MVC中创建一个带有自定义扩展的HttpHandler吗?我想创建一个具有以下路径的图像处理程序(如果可能的话),例如:domain.com/picture/{id of the picture}。这是否可以在MVC内部完成,或者我必须在IIS 7中做?
plaintext是的,您可以在MVC中创建一个自定义的HttpHandler来处理图像请求。通过继承`System.Web.HttpHandler`类并重写相应的方法,您可以自定义处理程序以支持图像路径。以下是一个简单的示例:
csharppublic class ImageHandler : IHttpHandler{ public bool IsReusable { get { return true; } }
public void ProcessRequest(HttpContext context) { string imagePath=context.Request.PathInfo.TrimStart('/').Split('/')[1]; string physicalPath=context.Server.MapPath(imagePath);
if (System.IO.File.Exists(physicalPath)) { context.Response.ContentType=image/ + physicalPath.Split('.').Last(); context.Response.WriteFile(physicalPath); } else { context.Response.StatusCode=404; } }}
您需要在MVC项目中注册这个处理程序,通常在`Global.asax`文件中添加如下代码:
csharpprotected void Application_Start(){ RouteTable.Routes.MapHttpHandler(image, typeof(ImageHandler));}
这样,当请求到类似`domain.com/picture/123`的URL时,就会调用您自定义的图像处理程序。
请注意,此方法不需要在IIS中进行特殊配置,只要确保您的MVC项目配置正确,并且IIS已经设置为允许ASP.NET应用程序运行。如果图像文件位于应用程序根目录之外,您可能需要调整`MapPath`方法的调用以确保安全性和正确性。
你能在MVC中创建一个带自定义扩展的自定义httphandler吗?我想要一个具有以下路径的图像处理程序(如果可能),
domain.com/picture/{id of the picture}
是否有可能从MVC内部或我必须在IIS 7中做到这一点?
无需添加httphandler.你应该通过控制器在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; }
本文共计582个文字,预计阅读时间需要3分钟。
你可以在MVC中创建一个带有自定义扩展的HttpHandler吗?我想创建一个具有以下路径的图像处理程序(如果可能的话),例如:domain.com/picture/{id of the picture}。这是否可以在MVC内部完成,或者我必须在IIS 7中做?
plaintext是的,您可以在MVC中创建一个自定义的HttpHandler来处理图像请求。通过继承`System.Web.HttpHandler`类并重写相应的方法,您可以自定义处理程序以支持图像路径。以下是一个简单的示例:
csharppublic class ImageHandler : IHttpHandler{ public bool IsReusable { get { return true; } }
public void ProcessRequest(HttpContext context) { string imagePath=context.Request.PathInfo.TrimStart('/').Split('/')[1]; string physicalPath=context.Server.MapPath(imagePath);
if (System.IO.File.Exists(physicalPath)) { context.Response.ContentType=image/ + physicalPath.Split('.').Last(); context.Response.WriteFile(physicalPath); } else { context.Response.StatusCode=404; } }}
您需要在MVC项目中注册这个处理程序,通常在`Global.asax`文件中添加如下代码:
csharpprotected void Application_Start(){ RouteTable.Routes.MapHttpHandler(image, typeof(ImageHandler));}
这样,当请求到类似`domain.com/picture/123`的URL时,就会调用您自定义的图像处理程序。
请注意,此方法不需要在IIS中进行特殊配置,只要确保您的MVC项目配置正确,并且IIS已经设置为允许ASP.NET应用程序运行。如果图像文件位于应用程序根目录之外,您可能需要调整`MapPath`方法的调用以确保安全性和正确性。
你能在MVC中创建一个带自定义扩展的自定义httphandler吗?我想要一个具有以下路径的图像处理程序(如果可能),
domain.com/picture/{id of the picture}
是否有可能从MVC内部或我必须在IIS 7中做到这一点?
无需添加httphandler.你应该通过控制器在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; }

