2

I want to serve images from a central location on my server. My image tags look like this:

<img src='@Url.Action("Image", "Home", new { id = "tt2119396.jpg" })' />

and in my HomeController the (experimental) action looks like this:

    [AcceptVerbs(HttpVerbs.Get)]
    public FileResult Image(string id)
    {
        var path = @"D:\Dropbox\MovieDB\TitleImages\" + id;
        return new FileStreamResult(new FileStream(path, FileMode.Open), "image/jpeg");
    }

However, the action Image is never called. I put a breakpoint on the first line and it never fires.

My routing definition is very simple, I don't think that is the issue:

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

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}
hholtij
  • 2,906
  • 6
  • 27
  • 42
  • What's the network response for your image url? Check the debug network monitor. – Jasen Apr 21 '15 at 23:27
  • Fiddler shows 404 as response. – hholtij Apr 21 '15 at 23:47
  • If you're sure the route/url is correct you can try to clear cache as GET requests may be cached by the browser. – Jasen Apr 21 '15 at 23:52
  • Try it as `/Home/Image?id=tt2119396.jpg` – Jasen Apr 21 '15 at 23:55
  • Further experiments have shown that it works as expected if I change the action parameter `id` to an `int`. Is there a way to pass a string to the action? – hholtij Apr 22 '15 at 00:01
  • 2
    Perhaps it is the file extension that is being intercepted by IIS – Jasen Apr 22 '15 at 00:02
  • Yes, Jasen, you are right. Removing the file extension from the string does the trick. I can see how the dot might confuse the parser, so it makes sense. Thanks! if you make it a formal answer, I will upvote and accept. – hholtij Apr 22 '15 at 00:10
  • I think [this question](http://stackoverflow.com/questions/6971203/how-to-route-to-css-js-files-in-mvc-net) already answered this. I had a hunch until I found that post. – Jasen Apr 22 '15 at 00:15
  • Here's [another answer](http://stackoverflow.com/questions/11728846/dots-in-url-causes-404-with-asp-net-mvc-and-iis) but it seems what works depends on IIS and MVC version (_ugh_). I'd leave the file extension off unless you really need it. – Jasen Apr 22 '15 at 00:54

1 Answers1

1

This problem occurs when you are using a dot (.) in your parameter value. You could solve this by using either of the following methods:

1) Add a forward slash (/) at the end of the query string, which by using Url.Action could be done like this

<img src='@Url.Action("Image", "Home", new { id = "tt2119396.jpg/" })' />

2) Edit your site's http handler as suggested here: Dots in URL causes 404 with ASP.NET mvc and IIS

Community
  • 1
  • 1
Indregaard
  • 1,195
  • 1
  • 18
  • 26