我将我的网站升级为使用ASP.Net MVC从传统的ASP.Net网络表单.我正在使用MVC路由来重定向旧服务器的请求.aspx页面到他们的新控制器/动作类似功能:

        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()实例处理请求.您可以构建一个custom处理程序来处理图像URL.

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

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

    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 "";
        }
    }
}

Asp.net相关问答推荐

ASP.NET多行查询文本框中的多个名称并将结果添加到单个网格视图

如何将标头中的用户名/密码传递给 SOAP WCF 服务

带有 Web 套接字的 SignalR

ASP.NET 控件无法在 Visual Studio 2008 的代码隐藏中引用

如何判断 IIS 是处于 32 位还是 64 位模式

在视图 Mvc.net 中访问路由值

免费的 ASP.Net 和/或 CSS 主题

问题映射 HttpHandler --> HTTP Error 404 Not Found

每个 'HttpRequest' 在 ASP.NET 中都有自己的线程吗?

将数据表导出到 Excel 文件

等价于 ASP.NET Core 中的 Html.RenderAction

与 Twitter Bootstrap Bundle 的 ASP.NET MVC4

发布网站项目时临时路径太长

如何获取正在访问 ASP.NET 应用程序的当前用户?

对 ASP.NET 2.0 网页进行单元测试的最佳方法是什么?

在 Visual Studio 2010 中随机禁用编辑 aspx/ascx 文件?

如何将列表转换为数据表

解析器错误:此处不允许使用_Default,因为它没有扩展类System.Web.UI.Page和 MasterType 声明

System.Linq.IQueryable 不包含Where的定义

如何清除 System.Runtime.Caching.MemoryCache