Question

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?

Was it helpful?

Solution

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top