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

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

问题描述

我升级我的网站从传统的ASP.Net web表单使用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.png到<一个href=\"http://www.domain.com/content/images/image.png\">http://www.domain.com/content/images/image.png.

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

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

推荐答案

您不能这样做开箱即用的MVC框架。记得有路由和URL重写之间的差。路由映射每个请求的资源,和预期的资源是一块code的。

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()。你可以建立一个的自定义的处理程序来处理图像的URL。

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,新干线(图形/ {文件名},结果
           新ImageRouteHandler()));

这就是它。

这是你的 IRouteHandler 类看起来是这样的:

Here's what your IRouteHandler class looks like:

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天全站免登陆