[英]How do I route images using ASP.Net MVC routing?
我将网站升级为使用来自传统ASP.Net Web表单的ASP.Net MVC。 我正在使用MVC路由将旧.aspx页的请求重定向到其新的Controller / Action等效项:
routes.MapRoute(
"OldPage",
"oldpage.aspx",
new { controller = "NewController", action = "NewAction", id = "" }
);
这对于页面非常有效,因为它们直接映射到控制器和操作。 但是,我的问题是图像请求-我不确定如何重定向这些传入请求。
我需要将http://www.domain.com/graphics/image.png的传入请求重定向到http://www.domain.com/content/images/image.png 。
使用.MapRoute()
方法时,正确的语法是什么?
您不能使用MVC框架“开箱即用”执行此操作。 请记住,路由和URL重写之间是有区别的。 路由将每个请求映射到一个资源,而预期的资源就是一段代码。
但是,MVC框架的灵活性使您可以毫无问题地完成此操作。 默认情况下,当您调用routes.MapRoute()
,它将使用MvcRouteHandler()
的实例处理请求。 您可以构建自定义处理程序来处理图像网址。
创建一个实现IRouteHandler
的类(可能称为ImageRouteHandler)。
将映射添加到您的应用中,如下所示:
routes.Add("ImagesRoute", new Route("graphics/{filename}",
new ImageRouteHandler()));
而已。
这是您的IRouteHandler
类的样子:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Compilation;
using System.Web.Routing;
using System.Web.UI;
namespace MvcApplication1
{
public class ImageRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
string filename = requestContext.RouteData.Values["filename"] as string;
if (string.IsNullOrEmpty(filename))
{
// return a 404 HttpHandler here
}
else
{
requestContext.HttpContext.Response.Clear();
requestContext.HttpContext.Response.ContentType = GetContentType(requestContext.HttpContext.Request.Url.ToString());
// find physical path to image here.
string filepath = requestContext.HttpContext.Server.MapPath("~/test.jpg");
requestContext.HttpContext.Response.WriteFile(filepath);
requestContext.HttpContext.Response.End();
}
return null;
}
private static string GetContentType(String path)
{
switch (Path.GetExtension(path))
{
case ".bmp": return "Image/bmp";
case ".gif": return "Image/gif";
case ".jpg": return "Image/jpeg";
case ".png": return "Image/png";
default: break;
}
return "";
}
}
}
如果要使用ASP.NET 3.5 Sp1 WebForms进行此操作,则必须创建一个单独的ImageHTTPHandler,该图像实现IHttpHandler来处理响应。 本质上,您所需要做的就是将GetHttpHandler方法中当前包含的代码放入ImageHttpHandler的ProcessRequest方法中。 我还将将GetContentType方法移动到ImageHTTPHandler类中。 还添加一个变量来保存文件名。
然后,您的ImageRouteHanlder类如下所示:
public class ImageRouteHandler:IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
string filename = requestContext.RouteData.Values["filename"] as string;
return new ImageHttpHandler(filename);
}
}
您的ImageHttpHandler类将如下所示:
public class ImageHttpHandler:IHttpHandler
{
private string _fileName;
public ImageHttpHandler(string filename)
{
_fileName = filename;
}
#region IHttpHandler Members
public bool IsReusable
{
get { throw new NotImplementedException(); }
}
public void ProcessRequest(HttpContext context)
{
if (string.IsNullOrEmpty(_fileName))
{
context.Response.Clear();
context.Response.StatusCode = 404;
context.Response.End();
}
else
{
context.Response.Clear();
context.Response.ContentType = GetContentType(context.Request.Url.ToString());
// find physical path to image here.
string filepath = context.Server.MapPath("~/images/" + _fileName);
context.Response.WriteFile(filepath);
context.Response.End();
}
}
private static string GetContentType(String path)
{
switch (Path.GetExtension(path))
{
case ".bmp": return "Image/bmp";
case ".gif": return "Image/gif";
case ".jpg": return "Image/jpeg";
case ".png": return "Image/png";
default: break;
}
return "";
}
#endregion
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.