我的测试设置由一个带有 foreach 的视图组成,该视图迭代一个简单的模型,目的是为模型集合中的每个项目呈现图像。循环内部有两个 @Url.Action 助手,它们调用控制器中的 FileContentResult 方法,除了一个从视图中获取参数而另一个具有硬编码的参数变量之外,它们是相同的。
@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" />
}
在我的控制器中,两种方法是:
public FileContentResult GetFooImage() // variables hard coded in body
public FileContentResult GetFoobarImage(int id, string entity, string size)
GetFooImage() 返回一个图像。FileContentResult GetFoobarImage() 没有。
谜底如下:如果我在 GetFoobarImage 设置断点,它甚至都不会被命中。我不知道为什么 GetFooImage 被调用,但 GetFoobarImage 没有。