0

I have the following action in my controller within my Web API project.

 [System.Web.Http.RoutePrefix("api/Events")]
public class EventsController : ApiController
{
    [System.Web.Http.HttpGet]
    [System.Web.Http.Route("Images/{title}/{id}/{photoName}")]
    public HttpResponseMessage GetImage(string title, Guid id, string photoName)
    {
        var path = $"c:\\Photos\\{title}\\{id}\\{photoName}";
        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
        var stream = new FileStream(path, FileMode.Open, FileAccess.Read);
        result.Content = new StreamContent(stream);
        result.Content.Headers.ContentType =
            new MediaTypeHeaderValue("application/octet-stream");
        return result;
    }
}

Here is my WebApiConfig class.

public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }

I have set a breakpoint in this action of mine. The problem, the breakpoint is not being hit.

Here is the URL I am using:

http://localhost:52584/api/Events/Images/Test_Title/80b88b32-0e45-4b8f-9a63-61ee28bd11fd/Test.jpg

Is there a possibility that the breakpoint does not get hit, because I am passing the .jpg extension?

monstertjie_za
  • 7,277
  • 8
  • 42
  • 73

1 Answers1

1

With reference to this accepted answer, I could solve my issue with the following handler:

<system.webServer>
  <handlers>
    <add 
      name="ManageJPGHandler" 
      path="api/Events/Images/*/*/*.jpg" 
      verb="GET" 
      type="System.Web.Handlers.TransferRequestHandler" 
      preCondition="integratedMode,runtimeVersionv4.0"
    />
  </handlers>
</system.webServer>
monstertjie_za
  • 7,277
  • 8
  • 42
  • 73