User Agent Causes MVC DisplayFor ArgumentException: Illegal characters in path

后端 未结 2 1897
感动是毒
感动是毒 2020-12-16 17:29

I\'m having a problem where users on mobile devices are encountering an error in MVC that does not occur when viewing the site on a regular desktop. I can consistently repr

相关标签:
2条回答
  • 2020-12-16 17:32

    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.

    0 讨论(0)
  • 2020-12-16 17:54

    I believe that the reason we are having this issue is MultipleViews and DisplayMode providers introduced for handling mobile devices separately. This is code that is still in .NET 4 but has been deprecated.

    A solution to disable the functionality entirely can be found here. Illegal characters in path depending on User-Agent?

    0 讨论(0)
提交回复
热议问题