gpt4 book ai didi

c# - 用户代理导致 MVC DisplayFor ArgumentException : Illegal characters in path

转载 作者:可可西里 更新时间:2023-11-01 08:44:58 35 4
gpt4 key购买 nike

我遇到一个问题,移动设备上的用户在 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,将成功的请求与失败的请求进行比较时,请求的唯一区别是用户代理(以及 jQuery 作为查询字符串参数的一部分附加的缓存消除器)。

为什么只更改 UA 会导致此异常?如果不针对每个可能发生的地方在系统中编写特定的解决方法,我如何才能避免此问题?

最佳答案

我遇到了完全相同的问题,并已解决。

我的问题原来是在我的 View 模型中使用了 yield block :

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

查看:

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

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

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

大概编译器生成的表示 yield block 的类包含一些用户代理不喜欢的字符。包括 ToList() 调用使用 List<> 代替。

关于c# - 用户代理导致 MVC DisplayFor ArgumentException : Illegal characters in path,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27487032/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com