User Agent Causes MVC DisplayFor ArgumentException: Illegal characters in path
I had the exact same problem, and fixed it.
My problem turned out to be the use of a yield
block in my viewmodel:
Controller:
var vm = new BigVM {
SmallVMs = BuildSmallOnes()
};
return View(vm);
private IEnumerable<SmallVM> BuildSmallOnes()
{
// complex logic
yield return new SmallVM(1);
yield return new SmallVM(2);
}
View:
@model BigVM
@Html.DisplayFor(x => x.SmallVMs) <-- died
Inexplicably, this worked for desktops but failed for iPads and iPhones, citing the exact same stacktrace. Similar problems were reported here and here. The problem was solved by adding a .ToList()
call, thus:
var vm = new BigVM {
SmallVMs = BuildSmallOnes().ToList()
};
Presumably the class that the compiler generates to represent the yield block includes some characters that some User Agents just don't like. Including the ToList() call uses a List<> instead.