asp.net-mvcfilecontentresult

MVC Controller FileContentResult method not being called


My test setup is comprised of a view with a foreach that iterates over a simple model with the objective of rendering an image for each item in the model collection. Inside the loop are two @Url.Action helpers that call FileContentResult methods in the controller that are identical except that one takes parameters from the view and the other has the parameter variables hard coded.

@foreach (var i in Model.FeaturedItems)
{
<img src="@Url.Action("GetFooImage", "Home", new {})" alt="@i.Name" />
<img src="@Url.Action("GetFoobarImage", "Home", new {i.ItemID, Entity="item", Size="m"})" alt="@i.Name" />
}

In my controller the two methods are:

public FileContentResult GetFooImage() // variables hard coded in body
public FileContentResult GetFoobarImage(int id, string entity, string size)

GetFooImage() returns an image. FileContentResult GetFoobarImage() does not.

Here's the mystery: If I put a breakpoint at GetFoobarImage it doesn't even get hit. I can't figure out why GetFooImage gets called but GetFoobarImage does not.


Solution

  • Double check the Url.Action

    @Url.Action("GetFoobarImage", "Home", new {i.ItemID, Entity="item", Size="m"})"

    Your missing "id =" in your call. It should be

    @Url.Action("GetFoobarImage", "Home", new {id = i.ItemID, Entity="item", Size="m"})
    

    Also double check your signature. There is an issue with Entity in the Url.Action matching up with Entiry in your method declaration. Not sure if this is a typo in your example.