7

The following code gives a NULL reference error when str is NULL, but only on our production server, not on our development or testing servers.

<div>
<input value="@Html.Raw(str)" />
</div>

This works just fine:

<div>
<input value="@str" />
</div>

As does this:

<div>
Values is: @Html.Raw(str)
</div>

So it's not an issue of @Html.Raw not being able to accept a NULL parameter. I'm not sure what exactly it returns in that case though; but it's somehow different on one server than on another.

Both are running the same version of System.Web.Mvc, though I don't know know what other DLLs to check. System.Web.WebPages, which is the assembly that should contain the MVC method that's crashing, is the same version on both servers. This is MVC version 4; .Net 4.5.

How is such a thing possible? The error occurs in the WriteAttributeTo method, though I don't even know why that would be called here, since it's using standard HTML input tags, not the @Html.TextBoxFor helper.

Edit One thing that might be helpful is if others try putting that code in a View of their own to see if it works or breaks. At least then I would know if it's my production environment or the testing environment that's behaving unexpectedly.

Here is the full stack trace:

at System.Web.WebPages.WebPageExecutingBase.WriteAttributeTo(String pageVirtualPath, TextWriter writer, String name, PositionTagged1 prefix, PositionTagged1 suffix, AttributeValue[] values) at System.Web.WebPages.WebPageExecutingBase.WriteAttributeTo(TextWriter writer, String name, PositionTagged1 prefix, PositionTagged1 suffix, AttributeValue[] values) at System.Web.WebPages.WebPageExecutingBase.WriteAttribute(String name, PositionTagged1 prefix, PositionTagged1 suffix, AttributeValue[] values) at ASP._Page_Views_AdminSurvey_TopDown_cshtml.Execute() in c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\fdd62ffd\90d151d1\App_Web_xsolwitr.2.cs:line 0 at System.Web.WebPages.WebPageBase.ExecutePageHierarchy() at System.Web.Mvc.WebViewPage.ExecutePageHierarchy() at System.Web.WebPages.StartPage.ExecutePageHierarchy() at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) at System.Web.Mvc.ControllerActionInvoker.<>c_DisplayClass1a.b_17() at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func1 continuation) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func1 continuation) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c_DisplayClass25.b_22(IAsyncResult asyncResult) at System.Web.Mvc.Controller.<>c_DisplayClass1d.b_18(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.<>c_DisplayClass4.b_3(IAsyncResult ar) at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.<>c_DisplayClass4.b_3(IAsyncResult ar) at System.Web.Mvc.MvcHandler.<>c_DisplayClass6.<>c_DisplayClassb.b_4(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.<>c_DisplayClass4.b__3(IAsyncResult ar) at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

4

4 回答 4

5

多亏了 nemesv 和 Dave A 的帮助,我才能够找到问题所在。这是一个两部分的答案。

1)出现异常的原因似乎是Razor实现中在HTML属性Html.Raw内进行组合时存在错误。当Razor 视图引擎遇到HTML属性中的文本时,它会使用该方法,即使它只是在纯HTML中。以前我的印象是Razor 视图引擎只处理服务器端命令(符号后面的代码)。尽管两者都允许null 值,但需要特殊处理来处理 2 的组合,并且如果存在 null 值,则该代码会崩溃。WebPageExecutingBase.WriteAttributeTo()@Html.RawWriteAttibuteTo()

2)这已经在System.Web.WebPages.dll的最新版本中更新/修复,这导致了答案的第二部分。该项目引用了该dll的Ver 2.0.20505.0,并将其部署在项目的bin文件夹中。此版本包含错误。

但即使它被项目引用,并且在 bin 文件夹中,测试服务器也没有使用该版本的 DLL 进行程序集绑定。fuslog.exe显示它正在绑定一个在GAC中注册的程序集,即Ver 2.0.20710.0。QA 服务器在 GAC 中有这个,而生产服务器没有。我不知道GAC会覆盖 bin 文件夹中的 dll。

我更新了我的项目以引用最新的ASPNetMVC包,其中包括WebPages dllVer 2.0.30506.0

于 2013-10-31T15:31:10.987 回答
2

这种微小的内部版本号差异实际上会产生影响吗?

当然可以,次要构建通常包含非破坏性更改,但这并不意味着它们不能包含错误。看起来问题正在发生,System.Web.WebPages.WebPageExecutingBase因此可以安全地假设System.Web.dll版本不匹配可能是问题的根源。

您可以通过反汇编程序运行这两个 DLL,并查看该区域及其周围是否有任何明显的变化,但是,如果您知道该问题在以后的构建中已解决,那么只需在您的部署机器上使用它即可。


在 8 月修复的属性中使用时出现与未正确解码文本有关的问题Html.Raw,不确定这是否是解决问题的 2 个版本之间的更改。

于 2013-10-29T22:13:39.417 回答
1

让我们深入研究 Razor 视图引擎:

当您的操作方法调用时

return View()

View()方法返回一个ViewResult对象。ViewResult对象有一个方法——它ExecuteResult()负责创建HTML文档。

ExecuteResult()方法遇到一个 Helper like@Html.Raw时,它​​会检查上下文。在填充属性的情况下,Razor 视图引擎通过遵循规则并避免陷入困境来尝试变得智能(或不愚蠢)。用于属性值渲染的方法是WebPageExecutingBase.WriteAttributeTo().

如果您查找WriteAttributeTo()的签名,您会发现它需要ParamArray最后一个。同样,我们正在研究抽象方法(未发布),但我怀疑WriteAttributeTo()方法的实现没有考虑空数组并引发异常。

于 2013-10-30T00:20:58.063 回答
0

非常有趣的问题,我很高兴你找到了答案!

如果由于某种原因您被旧版本的 DLL 卡住,这只是一个快速修复 - 使用 null 合并运算符??来确保内容永远不会null

<div>
    <input value="@Html.Raw(str ?? "")" />
</div>
于 2013-10-31T19:45:18.017 回答