如何使用ASP.Net MVC路由路由图像?

The*_*att 35 c# asp.net asp.net-mvc routing url-rewriting

我将我的站点升级为使用传统ASP.Net webforms中的ASP.Net MVC.我正在使用MVC路由将旧.aspx页面的请求重定向到它们的新Controller/Action等效项:

        routes.MapRoute(
            "OldPage",
            "oldpage.aspx",
            new { controller = "NewController", action = "NewAction", id = "" }
        );
Run Code Online (Sandbox Code Playgroud)

这对于页面非常有用,因为它们直接映射到控制器和操作.但是,我的问题是图像请求 - 我不知道如何重定向这些传入的请求.

我需要将http://www.domain.com/graphics/image.png的传入请求重定向到http://www.domain.com/content/images/image.png.

使用该.MapRoute()方法时的正确语法是什么?

wom*_*omp 50

你不能用MVC框架"开箱即用".请记住,路由和URL重写之间存在差异.路由将每个请求映射到资源,预期资源是一段代码.

但是 - MVC框架的灵活性允许您在没有实际问题的情况下执行此操作.默认情况下,当您调用时routes.MapRoute(),它使用实例处理请求MvcRouteHandler().您可以构建自定义处理程序来处理图像URL.

  1. 创建一个实现的类,可能称为ImageRouteHandler IRouteHandler.

  2. 将映射添加到您的应用程序,如下所示:

    routes.Add("ImagesRoute", new Route("graphics/{filename}",
    new ImageRouteHandler()));

  3. 而已.

这是你的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 "";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我最后写了博客:http://www.phpvs.net/2009/08/06/aspnet-mvc-how-to-route-to-images-or-other-file-types/ (3认同)

awr*_*t18 6

如果您使用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);

        }
    } 
Run Code Online (Sandbox Code Playgroud)

而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
    }
Run Code Online (Sandbox Code Playgroud)