Razor助手中的奇怪NullReferenceException

时间:2013-11-12 17:26:48

标签: c# razor webforms razor-2

我在C# Web表单 应用程序中使用Razor Helpers。

以下代码在调用时编译并呈现A-OK:

@helper MemberListItem(string firstname, string lastname, string avatarUrl)
{
    <li>
        @firstname @lastname
        @avatarUrl
    </li>
}

输出(两次调用助手):

<li>Bryan Arnold ../../Resources/Images/Placeholders/generic-user-image.jpg</li>
<li>Doug Bland ../../Resources/Images/Placeholders/generic-user-image.jpg</li>

但是当我更改帮助器以便将avatarUrl放置在src标记的img属性中时,我会在NullReferenceException上获得firstname。是的,NullReferenceException位于firstname

以下代码编译但在调用时抛出NRE:

@helper MemberListItem(string firstname, string lastname, string avatarUrl)
{
    <li>
        @firstname @lastname
        <img src="@avatarUrl"/>
    </li>
}

请记住,除了@avatarUrl在帮助程序中的位置之外,我不会更改任何内容。

如何显示图像?

更新1: 我还尝试在img中包装我的<text></text>代码(无效):

<li>
    @firstname @lastname
    <text>
        <img src="@avatarUrl"/>
    </text>
</li>

更新2: 这是错误:

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 

Line 11: {
Line 12:     <li>
Line 13:         @firstname @lastname
Line 14:         <img src="@avatarUrl"/>
Line 15:     </li>

Source File: RazorHelpers\Family.cshtml    Line: 13 

更新3: 我忘了提到我在一个单独的文件中定义@helper。然后,我从这样的aspx模板调用那个帮助器:

<%= @HelperFile.Helper(parameters).ToString() %>

另外,我认为@Luaan可能会有所作为。我试图摆弄~用于相对路径到我的图像文件,认为Razor可能会抛出一个NRE,因为它无法找到该文件。似乎没有~的设置在我的项目中有效。

使用静态文件路径:

这有效:

<img src="/Resources/Images/Placeholders/generic-user-image.jpg"/>

这不是(NullReferenceException):

<img src="~/Resources/Images/Placeholders/generic-user-image.jpg"/>

使用动态文件路径:

这不起作用(NullReferenceException):

<img src="@avatarUrl"/>

这两者都没有(NullReferenceException):

<img src="~@avatarUrl"/>

注意:Resources目录位于我的网站的根目录中。

更新4:

由于我的项目时间限制,我抛弃了我的原始助手(在本问题开头提到的那个)并且使用了纯ASPX模板。但是,我试图在这个应用程序中使用Razor帮助器来实现不同的功能,我遇到了我之前描述的相同问题。

这是一个类似的帮助器(源代码和编译代码)。帮助程序应该显示一个链接列表,允许用户使用各种身份提供程序(谷歌,脸书,推特,雅虎)注册/登录我的应用程序。这里的故事是一样的;当我打印img src时,帮助程序执行时没有错误,但是当我将img src放入实际的<img/>标记时,它会抛出NullReferenceException。

来源:

@helper ListGroupGrid(IEnumerable<ExternalIdentityProvider> providers) {
    <div>
        @foreach(var provider in providers){
            @provider.Name
            <img src="@provider.IconUrl"/>
        }
    </div>
}

编译代码(来自c:\ Windows中的临时ASP.NET文件):

#pragma checksum "C:\Users\Public\Documents\VisualStudioProjects\OnlineStable\Aqha\App_Code\RazorHelpers\IdentityProvider.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "46B0FEE2042706017F4AE53D4EA612F3E73EDF8B"
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.18052
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace ASP.RazorHelpers {
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Web;
    using System.Web.Helpers;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.WebPages;
    using System.Web.WebPages.Html;

    #line 1 "C:\Users\Public\Documents\VisualStudioProjects\OnlineStable\Aqha\App_Code\RazorHelpers\IdentityProvider.cshtml"
    using Aqha.DatabaseHelpers;

    #line default
    #line hidden

    #line 2 "C:\Users\Public\Documents\VisualStudioProjects\OnlineStable\Aqha\App_Code\RazorHelpers\IdentityProvider.cshtml"
    using Aqha.RazorExtensions;

    #line default
    #line hidden

    #line 3 "C:\Users\Public\Documents\VisualStudioProjects\OnlineStable\Aqha\App_Code\RazorHelpers\IdentityProvider.cshtml"
    using DevExpress.Utils.Drawing.Helpers;

    #line default
    #line hidden


    public class IdentityProvider : System.Web.WebPages.HelperPage {

#line hidden

#line 5 "C:\Users\Public\Documents\VisualStudioProjects\OnlineStable\Aqha\App_Code\RazorHelpers\IdentityProvider.cshtml"
public static System.Web.WebPages.HelperResult ListGroupGrid(IEnumerable<IdentityProviderData.ExternalIdentityProvider> providers) {
#line default
#line hidden
return new System.Web.WebPages.HelperResult(__razor_helper_writer => {

#line 5 "C:\Users\Public\Documents\VisualStudioProjects\OnlineStable\Aqha\App_Code\RazorHelpers\IdentityProvider.cshtml"



#line default
#line hidden
BeginContext(__razor_helper_writer, "~/App_Code/RazorHelpers/IdentityProvider.cshtml", 202, 11, true);

WriteLiteralTo(__razor_helper_writer, "    <div>\r\n");

EndContext(__razor_helper_writer, "~/App_Code/RazorHelpers/IdentityProvider.cshtml", 202, 11, true);


#line 7 "C:\Users\Public\Documents\VisualStudioProjects\OnlineStable\Aqha\App_Code\RazorHelpers\IdentityProvider.cshtml"


#line default
#line hidden

#line 7 "C:\Users\Public\Documents\VisualStudioProjects\OnlineStable\Aqha\App_Code\RazorHelpers\IdentityProvider.cshtml"
         foreach(var provider in providers){


#line default
#line hidden
BeginContext(__razor_helper_writer, "~/App_Code/RazorHelpers/IdentityProvider.cshtml", 272, 13, false);


#line 8 "C:\Users\Public\Documents\VisualStudioProjects\OnlineStable\Aqha\App_Code\RazorHelpers\IdentityProvider.cshtml"
WriteTo(__razor_helper_writer, provider.Name);


#line default
#line hidden
EndContext(__razor_helper_writer, "~/App_Code/RazorHelpers/IdentityProvider.cshtml", 272, 13, false);


#line 8 "C:\Users\Public\Documents\VisualStudioProjects\OnlineStable\Aqha\App_Code\RazorHelpers\IdentityProvider.cshtml"



#line default
#line hidden
BeginContext(__razor_helper_writer, "~/App_Code/RazorHelpers/IdentityProvider.cshtml", 287, 16, true);

WriteLiteralTo(__razor_helper_writer, "            <img");

EndContext(__razor_helper_writer, "~/App_Code/RazorHelpers/IdentityProvider.cshtml", 287, 16, true);

WriteAttributeTo(__razor_helper_writer, "src", Tuple.Create(" src=\"", 303), Tuple.Create("\"", 326)

#line 9 "C:\Users\Public\Documents\VisualStudioProjects\OnlineStable\Aqha\App_Code\RazorHelpers\IdentityProvider.cshtml"
, Tuple.Create(Tuple.Create("", 309), Tuple.Create<System.Object, System.Int32>(provider.IconUrl

#line default
#line hidden
, 309), false)
);

BeginContext(__razor_helper_writer, "~/App_Code/RazorHelpers/IdentityProvider.cshtml", 327, 4, true);

WriteLiteralTo(__razor_helper_writer, "/>\r\n");

EndContext(__razor_helper_writer, "~/App_Code/RazorHelpers/IdentityProvider.cshtml", 327, 4, true);


#line 10 "C:\Users\Public\Documents\VisualStudioProjects\OnlineStable\Aqha\App_Code\RazorHelpers\IdentityProvider.cshtml"
        }


#line default
#line hidden
BeginContext(__razor_helper_writer, "~/App_Code/RazorHelpers/IdentityProvider.cshtml", 342, 12, true);

WriteLiteralTo(__razor_helper_writer, "    </div>\r\n");

EndContext(__razor_helper_writer, "~/App_Code/RazorHelpers/IdentityProvider.cshtml", 342, 12, true);


#line 12 "C:\Users\Public\Documents\VisualStudioProjects\OnlineStable\Aqha\App_Code\RazorHelpers\IdentityProvider.cshtml"


#line default
#line hidden
});

#line 12 "C:\Users\Public\Documents\VisualStudioProjects\OnlineStable\Aqha\App_Code\RazorHelpers\IdentityProvider.cshtml"
}
#line default
#line hidden


        public IdentityProvider() {
        }

        protected static ASP.global_asax ApplicationInstance {
            get {
                return ((ASP.global_asax)(Context.ApplicationInstance));
            }
        }
    }
}

更新5: 我做了一些测试。为了尽可能清楚地说明问题,我删除了所有输入参数和额外标记。

考虑以下三个助手:

@helper BaseCase() {
    <img src="/Resources/Images/Placeholders/generic-user-image.jpg"/>
}

@helper VariableBaseCase() {
    var src = "/Resources/Images/Placeholders/generic-user-image.jpg";
    <img src="/Resources/Images/Placeholders/generic-user-image.jpg"/>
    <text>the source is @src</text>
}

@helper Variable() {
    var src = "/Resources/Images/Placeholders/generic-user-image.jpg";
    <img src="@src"/>
    <text>the source is @src</text>
}

当我从ASPX调用前两个助手时,他们无例外地显示输出(并且图像正常显示):

<%= RazorHelpers.ImageTest.BaseCase().ToString() %>
<%= RazorHelpers.ImageTest.VariableBaseCase().ToString() %>

当我调用最后一个助手时,我得到了同样的错误。

<%= RazorHelpers.ImageTest.Variable().ToString() %>

我希望现在我认为这在某种程度上是一个相对路径问题是正确的。如何让Razor在我的Web窗体应用程序中显示具有动态src属性的图像?

6 个答案:

答案 0 :(得分:2)

我遇到了类似的问题(mvc.net 4)。 我以这种方式绕过它:

错误代码:

<input name="ItemUID" type="hidden" value="@ItemUID" />

工作代码:

<input name="ItemUID" type="hidden" @("value=")"@ItemUID" />

答案 1 :(得分:1)

Razor解析器无法正确解析HTML,因为它一次查找部分。 在这种情况下,它假定整个block()是HTML

尝试使用&lt;文本&GT; HTML here &lt; / text&gt; 使解析器更容易阅读和解释

答案 2 :(得分:1)

我相信Razor会尝试确保您传递给img src的网址有效。您确定文件实际存在于正确的相对目录中吗? 也许您可以尝试以root相对格式传递URL(即"~/Resources/Images/Placeholders/generic-user-image.jpg")。 如果这没有帮助,您可以尝试使用@ Html.Image或@ Html.Raw渲染图像。 如果这没有帮助,请尝试@ Url.Content(avatarUrl),但这真的很难:)

答案 3 :(得分:1)

看来你不是唯一有问题的人。你MVC版本(MVC2我假设)有一个错误,当它调用WriteAttributeTo时会引发异常。以下stackoverflow问题&#34; Null reference exception in WriteAttributeTo method&#34;。

中提到了此问题

唯一合理的解决方案是使用更新的版本。可能是MVC3(带有update1)。你的.NET版本是4.0+所以它不会有问题。

答案 4 :(得分:1)

这在某个地方响起了铃铛。尝试下面的变量周围的括号,似乎有点帮助Razor引擎:

@helper MemberListItem(string firstname, string lastname, string avatarUrl)
{
    <li>
        @(firstname) @(lastname)
        <img src="@(avatarUrl)" />
    </li>
}

答案 5 :(得分:0)

有点晚了,但是我遇到了同样的问题,花了我几个小时才能解决。 对我而言,解决此问题的原因如下:

string attributeValue= "some value";

string attributeExpression= "attributeName=" + attributeValue;

<div class="" id="" @attributeExpression></div>