Problem with Custom Routes and HttpHandler in ASP.NET MVC

3

In a project I'm working on, I wrote a HttpHandler to get an image instead of a View , 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 go to the following address:

  

link

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

What should I do?

    
asked by anonymous 16.04.2014 / 17:38

1 answer

0

I asked the same question in the gringo OS and they told me by comment that the approach should be changed. It does not require HttpHandlers , modify routes or anything of the type. Simply doing Controller returning a FileResult already 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);
}
    
05.05.2014 / 23:42