如何使用 ASP.Net MVC 路由来路由图像? [英] How do I route images using ASP.Net MVC routing?

查看:15
本文介绍了如何使用 ASP.Net MVC 路由来路由图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将我的网站升级为使用来自传统 ASP.Net 网络表单的 ASP.Net MVC.我正在使用 MVC 路由将旧 .aspx 页面的请求重定向到它们的新控制器/操作等效项:

I upgraded my site to use ASP.Net MVC from traditional ASP.Net webforms. I'm using the MVC routing to redirect requests for old .aspx pages to their new Controller/Action equivalent:

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

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

This is working great for pages because they map directly to a controller and action. However, my problem is requests for images - I'm not sure how to redirect those incoming requests.

我需要重定向对 http://www.domain.com/graphics/image 的传入请求.pnghttp://www.domain.com/content/images/图像.png.

I need to redirect incoming requests for http://www.domain.com/graphics/image.png to http://www.domain.com/content/images/image.png.

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

What is the correct syntax when using the .MapRoute() method?

推荐答案

您不能使用 MVC 框架开箱即用"地做到这一点.请记住,路由和 URL 重写之间存在差异.路由就是把每一个请求都映射到一个资源上,期望的资源就是一段代码.

You can't do this "out of the box" with the MVC framework. Remember that there is a difference between Routing and URL-rewriting. Routing is mapping every request to a resource, and the expected resource is a piece of code.

然而 - MVC 框架的灵活性使您可以毫无问题地做到这一点.默认情况下,当您调用 routes.MapRoute() 时,它会使用 MvcRouteHandler() 的实例处理请求.您可以构建一个自定义处理程序来处理您的图片网址.

However - the flexibility of the MVC framework allows you to do this with no real problem. By default, when you call routes.MapRoute(), it's handling the request with an instance of MvcRouteHandler(). You can build a custom handler to handle your image urls.

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

像这样将映射添加到您的应用:

Add the mapping to your app like this:

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 MVC 路由来路由图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆