c# – Problem with Custom Routes and HttpHandler in ASP.NET MVC

Question:

In a project I'm working on, I wrote an HttpHandler to bring up an image instead of a View as an answer, something like this:

using System;
using System.Web;
using System.Web.Routing;
using MeuProjeto.Data;
using MeuProjeto.Infrastructure;

namespace MeuProjeto.HttpHandlers
{
    public class ImagemFuncionarioHttpHandler : IHttpHandler
    {
        public RequestContext RequestContext { get; set; }

        public ImagemFuncionarioHttpHandler(RequestContext requestContext)
        {
            RequestContext = requestContext;
        }

        /// <summary>
        /// 
        /// </summary>
        public bool IsReusable
        {
            get { return false; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            var currentResponse = HttpContext.Current.Response;

            currentResponse.ContentType = "image/jpeg";
            currentResponse.Buffer = true;

            var usuarioId = Convert.ToInt32(RequestContext.RouteData.GetRequiredString("id"));

            try
            {
                var funcionario = new Funcionarios(GeneralSettings.DataBaseConnection).SelecionarPorId(usuarioId);

                currentResponse.BinaryWrite(funcionario.ThumbnailFuncionario);
                currentResponse.Flush();
            }
            catch (Exception ex)
            {
                context.Response.Write(ex.StackTrace);
            }
        }
    }
}

To work with a route, this HttpHandler needs a RouteHandler :

using System.Web;
using System.Web.Routing;
using MeuProjeto.HttpHandlers;

namespace MeuProjeto.RouteHandlers
{
    public class ImagemFuncionarioRouteHandler : IRouteHandler 
    {
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            return new ImagemFuncionarioHttpHandler(requestContext);
        }
    }
}

And the route configuration looks like this:

using System.Web.Mvc;
using System.Web.Routing;
using MeuProjeto.Helpers;
using MeuProjeto.RouteHandlers;

namespace MeuProjeto
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.Add(new Route("Funcionarios/Thumbnail/{id}", new ImagemFuncionarioRouteHandler()));

            routes.MapRouteWithName(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}

The code above works perfectly if I enter the following address:

http://meudominio.com.br/Funcionarios/Thumbnail/1

But every time I use the route on any screen whatsoever, the forms ' actions spoil, redirecting the POST request to this address http://meudominio.com.br/Funcionarios/Thumbnail/1?action=Index&controller=Vagas .

What should I do?

Answer:

I asked the same question on the gringo OS and I was told by comment that the approach should be changed. No need for HttpHandlers , modifying routes or anything like that. Simply making the Controller return a FileResult solves the problem:

public FileResult Thumbnail(int id)
{
    var funcionario = new Funcionarios(GeneralSettings.DataBaseConnection).SelecionarPorId(id);
    return File(funcionario.ThumbnailFuncionario, System.Net.Mime.MediaTypeNames.Image.Jpeg);
}
Scroll to Top