Jon*_*son 16 c# webforms razor razor-2
我在C#Web Forms应用程序中使用Razor Helpers .
以下代码在调用时编译并呈现A-OK:
@helper MemberListItem(string firstname, string lastname, string avatarUrl)
{
<li>
@firstname @lastname
@avatarUrl
</li>
}
Run Code Online (Sandbox Code Playgroud)
输出(两次调用助手):
<li>Bryan Arnold ../../Resources/Images/Placeholders/generic-user-image.jpg</li>
<li>Doug Bland ../../Resources/Images/Placeholders/generic-user-image.jpg</li>
Run Code Online (Sandbox Code Playgroud)
但是,当我改变助手,以便avatarUrl被放置在src一个属性img标签,我得到一个NullReferenceException上firstname.是的,NullReferenceException是的firstname.
以下代码编译但在调用时抛出NRE:
@helper MemberListItem(string firstname, string lastname, string avatarUrl)
{
<li>
@firstname @lastname
<img src="@avatarUrl"/>
</li>
}
Run Code Online (Sandbox Code Playgroud)
请记住,除了帮助程序中的位置之外,我不会更改任何内容@avatarUrl.
如何显示图像?
更新1:
我也试过包装我的img代码<text></text>(无济于事):
<li>
@firstname @lastname
<text>
<img src="@avatarUrl"/>
</text>
</li>
Run Code Online (Sandbox Code Playgroud)
更新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
Run Code Online (Sandbox Code Playgroud)
更新3: 我忘了提到我在一个单独的文件中定义@helper.然后,我从这样的aspx模板调用该帮助器:
<%= @HelperFile.Helper(parameters).ToString() %>
Run Code Online (Sandbox Code Playgroud)
此外,我认为@Luaan可能会有所作为.我试图摆弄~相对路径到我的图像文件,认为Razor可能会抛出一个NRE,因为它找不到文件.看来~我的项目中没有设置工作.
使用静态文件路径:
这有效:
<img src="/Resources/Images/Placeholders/generic-user-image.jpg"/>
Run Code Online (Sandbox Code Playgroud)
这不(NullReferenceException):
<img src="~/Resources/Images/Placeholders/generic-user-image.jpg"/>
Run Code Online (Sandbox Code Playgroud)
使用动态文件路径:
这不起作用(NullReferenceException):
<img src="@avatarUrl"/>
Run Code Online (Sandbox Code Playgroud)
这也不是(NullReferenceException):
<img src="~@avatarUrl"/>
Run Code Online (Sandbox Code Playgroud)
注意:该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>
}
Run Code Online (Sandbox Code Playgroud)
编译代码(来自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));
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
更新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>
}
Run Code Online (Sandbox Code Playgroud)
当我从ASPX调用前两个帮助器时,它们显示输出(并且图像正常显示),无异常:
<%= RazorHelpers.ImageTest.BaseCase().ToString() %>
<%= RazorHelpers.ImageTest.VariableBaseCase().ToString() %>
Run Code Online (Sandbox Code Playgroud)
当我打电话给最后一个助手时,我得到了同样的错误.
<%= RazorHelpers.ImageTest.Variable().ToString() %>
Run Code Online (Sandbox Code Playgroud)
我希望现在我认为这在某种程度上是一个相对路径问题是正确的.如何让Razor src在我的Web窗体应用程序中显示具有动态属性的图像?
Razor 解析器不会正确解析您的 HTML,因为它一次查找部分。在这种情况下,它假设整个块 () 是 HTML
尝试用 <text> HTML here </text> 包围您的 HTML 使得解析器更容易阅读和解释
| 归档时间: |
|
| 查看次数: |
2572 次 |
| 最近记录: |