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:
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?