Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 308649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:40:11+00:00 2026-05-12T07:40:11+00:00

I upgraded my site to use ASP.Net MVC from traditional ASP.Net webforms. I’m using

  • 0

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.

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

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T07:40:12+00:00Added an answer on May 12, 2026 at 7:40 am

    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.

    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. Create a class, maybe called ImageRouteHandler, that implements IRouteHandler.

    2. Add the mapping to your app like this:

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

    3. That’s it.

    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 "";
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just upgraded my site from ASP.NET 3.5 to 4.0. I've been working through
I'm just begin upgrade my asp.net site from Webform to MVC 2. So far
I've got an old WebForms-based ASP.NET app that I have upgraded to ASP.NET 4.0
We've upgraded the asp.net runtime from 2.0 to 4.0 - turns out a vendor
I have been using web parts on my site since ASP.NET 2.0 came out.
We have a Management information system that is due to be upgraded/rewritten using asp.net
Im faced with an impending upgrade to an ASP.NET site and I am thinking
I've been dealing with a legacy asp.net 2.0 web site that was originally setup
I just upgraded my Web Site project from 2.0 to 3.5 to take advantage
We've recently upgraded from Visual SourceSafe to SVN based on recommendations on this site.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.