用户代理导致MVC DisplayFor ArgumentException:路径中的非法字符

时间:2014-12-15 15:09:08

标签: c# asp.net asp.net-mvc user-agent

我遇到一个问题,即移动设备上的用户遇到MVC中的错误,而这种错误在常规桌面上查看网站时不会发生。我可以使用Chrome的开发人员工具并应用除默认值之外的任何其他UA来始终如一地重现错误。

抛出的基础异常是: ArgumentException: Illegal characters in path. at System.IO.Path.CheckInvalidPathChars(String path, Boolean checkAdditional) at System.IO.Path.GetExtension(String path) at System.Web.WebPages.DefaultDisplayMode.TransformPath(String virtualPath, String suffix) at System.Web.WebPages.DefaultDisplayMode.GetDisplayInfo(HttpContextBase httpContext, String virtualPath, Func'2 virtualPathExists) at System.Web.WebPages.DisplayModeProvider.GetDisplayInfoForVirtualPath(String virtualPath, HttpContextBase httpContext, Func'2 virtualPathExists, IDisplayMode currentDisplayMode, Boolean requireConsistentDisplayMode) at System.Web.Mvc.VirtualPathProviderViewEngine.GetPathFromGeneralName(ControllerContext controllerContext, List'1 locations, String name, String controllerName, String areaName, String cacheKey, String[]& searchedLocations) at System.Web.Mvc.VirtualPathProviderViewEngine.GetPath(ControllerContext controllerContext, String[] locations, String[] areaLocations, String locationsPropertyName, String name, String controllerName, String cacheKeyPrefix, Boolean useCache, String[]& searchedLocations) at System.Web.Mvc.VirtualPathProviderViewEngine.FindPartialView(ControllerContext controllerContext, String partialViewName, Boolean useCache) at System.Web.Mvc.ViewEngineCollection.<>c__DisplayClass2.<FindPartialView>b__1(IViewEngine e) at System.Web.Mvc.ViewEngineCollection.Find(Func'2 lookup, Boolean trackSearchedPaths) at System.Web.Mvc.ViewEngineCollection.FindPartialView(ControllerContext controllerContext, String partialViewName) at System.Web.Mvc.Html.TemplateHelpers.ExecuteTemplate(HtmlHelper html, ViewDataDictionary viewData, String templateName, DataBoundControlMode mode, GetViewNamesDelegate getViewNames, GetDefaultActionsDelegate getDefaultActions) at System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData, ExecuteTemplateDelegate executeTemplate) at System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData) at System.Web.Mvc.Html.TemplateHelpers.TemplateFor[TContainer,TValue](HtmlHelper'1 html, Expression'1 expression, String templateName, String htmlFieldName, DataBoundControlMode mode, Object additionalViewData, TemplateHelperDelegate templateHelper) at System.Web.Mvc.Html.TemplateHelpers.TemplateFor[TContainer,TValue](HtmlHelper'1 html, Expression'1 expression, String templateName, String htmlFieldName, DataBoundControlMode mode, Object additionalViewData) at System.Web.Mvc.Html.DisplayExtensions.DisplayFor[TModel,TValue](HtmlHelper'1 html, Expression'1 expression)

使用fiddler,将成功的请求与失败的请求进行比较时,请求的唯一区别是User-Agent(以及作为查询字符串参数的一部分的jQuery附加的缓存占用者)。

为什么只更改UA导致此异常?如果没有为系统中的每个地方编写特定的工作,我怎样才能避免此问题?

2 个答案:

答案 0 :(得分:10)

我有完全相同的问题,并修复了它。

我的问题原来是在我的viewmodel中使用了yield块:

控制器:

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);
}

查看:

@model BigVM
@Html.DisplayFor(x => x.SmallVMs)   <-- died

令人费解的是,这适用于台式机,但iPad和iPhone失败,引用完全相同的堆栈跟踪。报告了类似问题herehere。通过添加.ToList()调用解决了该问题,因此:

var vm = new BigVM {
    SmallVMs = BuildSmallOnes().ToList()
};

据推测,编译器生成的用于表示yield块的类包括一些用户代理不喜欢的字符。包括ToList()调用使用List&lt;&gt;代替。

答案 1 :(得分:0)

我相信我们遇到此问题的原因是为分别处理移动设备引入了MultipleViews和DisplayMode提供程序。这是仍在.NET 4中的代码,但已弃用。

可以在此处找到完全禁用该功能的解决方案。 Illegal characters in path depending on User-Agent?