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

  • Home
  • SEARCH
  • 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 9049479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:24:33+00:00 2026-06-16T12:24:33+00:00

I have a project that requires changes in MVC’s default routing behavior for image

  • 0

I have a project that requires changes in MVC’s default routing behavior for image http requests.

For example, this sample from RouteConfig.cs

Route ImagesRoute = new Route("{controller}/{folderName}/{fileName}", new ImageRouteHandler());
string regexWindowsFilePattern = @"^([^\x00-\x1f\\?/%*:|" + "\"" + @"<>]+)\.(?:jpeg|jpg|tiff|gif|png)";

ImagesRoute.Constraints = new RouteValueDictionary { { "fileName", regexWindowsFilePattern } };
routes.Add(ImagesRoute);

should reroute

http://localhost/home/contents/image.jpg

to path on disk (c:\cache\[folderName][fileName]). The “rerouting” in my case is just writing the correct http response based on request. In one project (lets call it the “Test” project) this code triggers normal behavior: the GetHttpHandler method of ImageRouteHandler class gets hit and the image appears in browser, however in other project with identical code for RouteConfig.cs and ImageRouteHandler inserted GetHttpHandler simply does not fire which results in 404 NOT FOUND http error. This other project (the “destination” project) has almost the same configuration (I have checked the relevant differences) and have been run on the same IIS express server. Creating new project and populating in with contents of destination and test project results in normal behavior (i.e. the image is displayed in browser).
Any clue on what the solution might be?

update_1:
I forgot to mention that the action is not used deliberately. I have an html file that I must include into partial view which will render html file body. I don’t have control over how this file is created but I have a defined structure: html file with name [htmlFileName] and a resource folder name with name [htmlFileName].files. When I request a specific URL (say localhost/[Controller]/[Action]) the references to resources in HTML markup result in incorrect URLs (http:// localhost/[Controller]/[folderName]/[fileName]) so I need to rewrite these URLs so that the browser’s http image requests are prorerly answered. Thats why I think this custom handler is needed.

using System;
using System.Net;
using System.Web;
using System.IO;
using System.Web.Routing;

namespace MyProject.WebUI.Interface.Utility
{
    public class ImageRouteHandler : IRouteHandler
    {
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            string fileName = requestContext.RouteData.Values["fileName"] as string;
            string folderName = requestContext.RouteData.Values["folderName"] as string;

            if (string.IsNullOrEmpty(fileName))
            {
                // return a 404 NOT FOUND HttpHandler here
                requestContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.NotFound;
            }
            else
            {
                requestContext.HttpContext.Response.Clear();

                if (requestContext.HttpContext.Request.Url != null)
                {
                    requestContext.HttpContext.Response.ContentType =
                        GetContentType(requestContext.HttpContext.Request.Url.ToString());
                }
                else
                {
                    requestContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.NotFound;
                    requestContext.HttpContext.Response.End();
                }

                // find physical path to image here.  
                string filepath = @"c:\Cache" + @"\" + folderName + @"\" + fileName;

                /*If file exists send the response, otherwise set HTTP 404 NOT FOUND status code for response.*/
                if (File.Exists(filepath))
                {
                    requestContext.HttpContext.Response.WriteFile(filepath);
                    requestContext.HttpContext.Response.End();
                }
                else
                {
                    requestContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.NotFound;
                    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 ".jpeg": return "Image/jpg";
                case ".png": return "Image/png";
                default: break;
            }
            return "";
        }
    }
}
  • 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-06-16T12:24:34+00:00Added an answer on June 16, 2026 at 12:24 pm

    It appears to be that in one project there is a “modules” element in Web.config

    <modules runAllManagedModulesForAllRequests="true" />
    

    which allowed routing module to work properly with image requests. It is placed in Web.config by default and for some reason there is no such xml element in the other project. Though this is a solution for a problem I found a better one:

    <modules>
      <remove name="UrlRoutingModule-4.0" />
      <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
    </modules>
    

    This enables only one (instead of all) managed module for any type of requests. After adding these xml elements to Web.config the GetHttpHandler of ImageRouteHandler gets hit as intended.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a project that requires the following. Four arrays will be declared in
I have a project that requires some configuration files. I want to keep the
I have recently just created Java project using Eclipse that requires 2 JAR files
$(.container).hover( function(){ $(.child-1).hide(0); $(.child-2).show(0); },function(){ $(.child-1).show(0); $(.child-2).hide(0); }); A project I have requires that
I have a project (built from an AppFuse template) that requires Maven 2.2.1. So
In my Spring MVC project I have an update page for Class1 that must
I have my project using MVC and I've got my controller that instantiates a
I'm doing a project in seam that requires restful URLs. I have a view
I've just create a new MVC project, and have made no changes at all,
I have a project that requires a couple of animations, and was wondering if

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.