我在 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
属性中时,我得到一个on 。是的,开启。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 助手来实现不同的功能,并且遇到了与之前描述的相同的问题。
这是一个类似的助手(源代码和编译代码)。助手应该显示一个链接列表,允许用户使用各种身份提供者(google、facebook、twitter、yahoo)为我的应用程序注册/登录。这里的故事是一样的;当我只打印 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 中的 Temporary ASP.NET Files):
#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() %>
我希望现在我相信这在某种程度上是一个相对路径问题是正确的。如何让 Razorsrc
在我的 Web 窗体应用程序中显示具有动态属性的图像?