用户代理导致MVC DisplayFor的ArgumentException:路径中具有非法字符 [英] User Agent Causes MVC DisplayFor ArgumentException: Illegal characters in path
问题描述
我在哪里移动设备上的用户遇到的MVC错误常规桌面上查看该网站时不会出现问题。我可以使用Chrome的开发者工具和应用任何其他UA比默认的一贯重现错误。
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 reproduce the error by using Chrome's developer tools and applying any other UA than the default.
引发的潜在的例外是:的ArgumentException:路径中具有非法字符。
在System.IO.Path.CheckInvalidPathChars(字符串路径,布尔checkAdditional)
在System.IO.Path.GetExtension(字符串路径)
在System.Web.WebPages.DefaultDisplayMode.TransformPath(字符串virtualPath,字符串后缀)
在System.Web.WebPages.DefaultDisplayMode.GetDisplayInfo(HttpContextBase HttpContext的,字符串virtualPath,Func'2 virtualPathExists)
在System.Web.WebPages.DisplayModeProvider.GetDisplayInfoForVirtualPath(字符串virtualPath,HttpContextBase的HttpContext,Func'2 virtualPathExists,IDisplayMode currentDisplayMode,布尔requireConsistentDisplayMode)
在System.Web.Mvc.VirtualPathProviderViewEngine.GetPathFromGeneralName(ControllerContext controllerContext,List'1位置,字符串名称,字符串controllerName,字符串AREANAME,字符串cacheKey,字符串[]放; searchedLocations)
在System.Web.Mvc.VirtualPathProviderViewEngine.GetPath(ControllerContext controllerContext,字符串[]位置的String [] areaLocations,字符串locationsPropertyName,字符串名称,字符串controllerName,字符串cacheKey preFIX,布尔useCache将,的String []&安培; searchedLocations)
在System.Web.Mvc.VirtualPathProviderViewEngine.FindPartialView(ControllerContext controllerContext,字符串partialViewName,布尔useCache将)
在System.Web.Mvc.ViewEngineCollection<方式>&c__DisplayClass2 LT; FindPartialView> b__1(IViewEngine E)
在System.Web.Mvc.ViewEngineCollection.Find(Func'2查找,布尔trackSearchedPaths)
在System.Web.Mvc.ViewEngineCollection.FindPartialView(ControllerContext controllerContext,字符串partialViewName)
在System.Web.Mvc.Html.TemplateHelpers.ExecuteTemplate(HTML的HtmlHelper,可视数据的ViewDataDictionary,字符串TEMPLATENAME,DataBoundControlMode模式,GetViewNamesDelegate getViewNames,GetDefaultActionsDelegate getDefaultActions)
在System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HTML的HtmlHelper,ModelMetadata元数据,字符串htmlFieldName,字符串TEMPLATENAME,DataBoundControlMode模式,对象additionalViewData,ExecuteTemplateDelegate executeTemplate)
在System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HTML的HtmlHelper,ModelMetadata元数据,字符串htmlFieldName,字符串TEMPLATENAME,DataBoundControlMode模式,对象additionalViewData)
在System.Web.Mvc.Html.TemplateHelpers.TemplateFor [TContainer,TValue](HtmlHelper'1 HTML,防爆pression'1前pression,字符串TEMPLATENAME,字符串htmlFieldName,DataBoundControlMode模式,对象additionalViewData,TemplateHelperDelegate templateHelper )
在System.Web.Mvc.Html.TemplateHelpers.TemplateFor [TContainer,TValue](HtmlHelper'1 HTML,防爆pression'1前pression,字符串TEMPLATENAME,字符串htmlFieldName,DataBoundControlMode模式,对象additionalViewData)
在System.Web.Mvc.Html.DisplayExtensions.DisplayFor [的TModel,TValue](HtmlHelper'1 HTML,防爆pression'1前pression)
The underlying exception thrown is:
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)
使用招,在请求比较成功的一个失败的请求时,唯一的区别是用户代理(和的jQuery附加作为查询字符串参数的一部分缓存克星)。
Using fiddler, the only difference in requests when comparing a successful one to a failed request is the User-Agent (and the cache buster appended by jQuery as part of the query string parameters).
为什么只改变了UA造成这种例外,我怎么能避免这个问题,周围没有写具体的工作进入系统的每一个地方,这是有可能发生?
Why is only changing the UA causing this exception and how can I avoid this issue without writing a specific work around into the system for every place that this is and could occur?
推荐答案
我有完全相同的问题,并固定它。
I had the exact same problem, and fixed it.
我的问题竟然是使用收益率
块在我的视图模型:
My problem turned out to be the use of a yield
block in my viewmodel:
控制器:
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,理由是完全相同的堆栈跟踪。类似的问题报告<一个href=\"http://stackoverflow.com/questions/10324877/illegal-characters-in-path-error-when-using-skip-and-take-with-ienumerable\">here和这里。这个问题通过加入 .ToList()
通话,这样就解决了:
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编译器生成重新present类产量块包括一些字符,一些用户代理只是不喜欢。包括了ToList()调用使用List&LT;>而不是
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.
这篇关于用户代理导致MVC DisplayFor的ArgumentException:路径中具有非法字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!