4

In a project I'm working, I've wrote an HttpHandler to bring as response an image instead of a View, like that:

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

namespace MyProject.HttpHandlers
{
    public class PersonImageHttpHandler : IHttpHandler
    {
        public RequestContext RequestContext { get; set; }

        public PersonImageHttpHandler(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 person = new People(GeneralSettings.DataBaseConnection).SelectPorId(usuarioId);

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

This HttpHandler needs a RouteHandler:

using System.Web;
using System.Web.Routing;
using MyProject.HttpHandlers;

namespace MyProject.RouteHandlers
{
    public class PersonImageRouteHandler : IRouteHandler 
    {
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            return new PersonImageHttpHandler(requestContext);
        }
    }
}

Routes.cs is something like that:

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

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

            routes.Add(new Route("People/Thumbnail/{id}", new PersonImageRouteHandler()));

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

Works like a charm accessing the following address

http://mydomain.com/People/Thumbnail/1

But every time I use some request for this route, the actions from forms doesn't work, redirecting the POST request to this address: http://mydomain.com/People/Thumbnail/1?action=Index&controller=AnotherController.

What is wrong?

Leonel Sanches da Silva
  • 5,441
  • 9
  • 42
  • 57
  • 1
    Side note: consider using regular controller instead and return [File](http://msdn.microsoft.com/en-us/library/dd470835%28v=vs.118%29.aspx) from action... will make code more consistent and you'll not need to learn 2 different routing approaches... – Alexei Levenkov Apr 16 '14 at 16:59
  • I used this approach because I'm converting a legacy code to MVC. Any alternative approaches will be welcome. – Leonel Sanches da Silva Apr 16 '14 at 17:01
  • 1
    I had a similar problem. Since the handler is handled directly by ASP.Net, I lost the ability to use MVC dependency injection. I got MVC to handle the routing (http://petesdotnet.blogspot.ca/2009/09/generic-handlers-and-aspnet-routing.html), but got the same problem with the mapping of the new Route when using @Html.BeginForm in my Views. Like Cigano, my solution was to use an API controller instead – Andy Thomas Mar 02 '16 at 16:21

1 Answers1

0

As suggested by @AlexeiLevenkov, I abandoned that mixed Routes approach and resolved implementing PeopleController as below:

public FileResult Thumbnail(int id)
{
    var person = new People(GeneralSettings.DataBaseConnection).SelectById(id);
    return File(person.Thumbnail, System.Net.Mime.MediaTypeNames.Image.Jpeg);
}
Leonel Sanches da Silva
  • 5,441
  • 9
  • 42
  • 57