描述:编译服务此请求所需的资源时发生错误。编译器错误消息:CS1513:}预期

时间:2012-02-29 15:40:11

标签: c# html asp.net-mvc-2

这是我的HTML代码:

<span>
<%if (Model.Data.Service.Attachments.Count > 0)
{

%><h3>Downloads for your service:</h3> <%

foreach (var attach in Model.Data.Service.Attachments)
{
    %><%=attach.Name%>: https://<%= Request.Url.Host%> "/File/Download/" <%= attach.Id.ToString()%><br />
}<%

}%></span>

错误是说我错过了一个“{”,但我不相信这就是这里发生的事情

2 个答案:

答案 0 :(得分:1)

  

但我不相信这就是这里发生的事情

你最好相信编译器。我建议您正确缩进代码,因为很容易看到这样的语法错误:

<span>
<% if (Model.Data.Service.Attachments.Count > 0) { %>
    <h3>Downloads for your service:</h3> 
    <% foreach (var attach in Model.Data.Service.Attachments) { %>
        <%= attach.Name %>: https://<%= Request.Url.Host %> "/File/Download/" <%= attach.Id.ToString() %>
        <br />
    <% } %>
<% } %>
</span>

答案 1 :(得分:1)

你的foreach的右括号不是&lt; %%&gt;块。它必须是:

<span>
<%if (Model.Data.Service.Attachments.Count > 0)
{

%><h3>Downloads for your service:</h3> <%

foreach (var attach in Model.Data.Service.Attachments)
{
%><%=attach.Name%>: https://<%= Request.Url.Host%> "/File/Download/" <%= attach.Id.ToString()%><br />
<% }

}%></span>

或者为了让它更整洁一点尝试使用string.Format:

<span>
<%if (Model.Data.Service.Attachments.Count > 0)
{
%><h3>Downloads for your service:</h3> <%
foreach (var attach in Model.Data.Service.Attachments)
{
    %><%= string.Format("{0}: https://{1}/File/Download/{2}", attach.Name, Request.Url.Host, attach.Id) %><br />
<%}
}%></span>