User Agent Causes MVC DisplayFor ArgumentException: Illegal characters in path

萝らか妹 提交于 2019-11-29 06:19:27
CSJ

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!